一、拆分策略
- 如果一個開發人員負責一個模塊,我們采用公用配置(包括數據源、事務等)+每個系統模塊一個單獨配置文件(包括Dao、Service、Web控制器)的形式
- 如果是按照分層進行的分工,我們采用公用配置(包括數據源、事務等)+DAO Bean配置+業務邏輯Bean配置+Web控制器配置的形式
二、拆分方法
如果有多個配置文件需要載入,可以分別傳入多個配置文件名,或以String[]方式傳入多個配置文件名。或者還可以采用通配符(*)來加載多個具有一定命名規則的配置文件。如下
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");
建議通過通配符(*)的方式配置多個Spring配置文件,建議在給Spring配置文件命名時遵循一定的規律
此外,Spring配置文件本身也可以通過<beans>標簽下的import子元素導入其他配置文件,將多個配置文件整合到一起,形成一個完整的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>