Spring教程
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml", "dao.xml", "service.xml");ApplicationContext同时指定多个配置文件
如果配置文件内容太多,可以拆分成多个配置文件,如:将一个工程的配置文件拆分为dao.xml,service.xml,然后通过application.xml 引入
application.xml:
<?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.xsd"> <import resource="dao.xml"></import> <import resource="service.xml"></import> </beans>
dao.xml:
<?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.xsd"> <bean id="daoBean" class="spring.example.DaoBean"> </bean> </beans>service.xml:
<?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.xsd"> <bean id="serviceBean" class="spring.example.ServiceBean"> </bean> </beans>
使用的时候,只需要引入application.xml文件即可:
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");