spring configuration file splitting strategies and methods

A split strategy

  • If a developer is responsible for a module, we use a common configuration (including data sources, transactions, etc.) + each system module configuration in the form of a separate file (including Dao, Service, Web controller)
  • If it is stratified according to the division, we use the common configuration (including a data source, transaction, etc.) arranged Bean the DAO + + + business logic in the form of Web Bean configuration controller configuration

Second, the resolution process

If there are multiple configuration files to be loaded, you can pass multiple profiles names are, or String [] way incoming multiple profile name. Or it may also be employed a wildcard (*) to load a plurality of profiles having certain naming rules. as follows

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml", "applicationContext-dao.xml", "applicationContext-jdbc.xml");
String[] configs = {"applicationContext.xml", "applicationContext-dao.xml", "applicationContext-service.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(configs);
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext*.xml");

Recommended to configure multiple Spring configuration files by wildcard (*) way, it is recommended to follow certain rules in the configuration file name to Spring

Further, Spring configuration file itself may be introduced by other profiles <beans> child element under the import tab to integrate multiple configuration files together to form a complete profile Spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <import resource="classpath:applicationContext-dao.xml"/>
    <import resource="classpath:applicationContext-service.xml"/>

</beans>

Guess you like

Origin www.cnblogs.com/yanguobin/p/11704654.html