Spring教程
作者: 时海 hadoop迷
配置文件那些事

1、同时指定多个配置文件

 ApplicationContext context = new ClassPathXmlApplicationContext("application.xml", "dao.xml", "service.xml");
ApplicationContext同时指定多个配置文件

2、配置文件拆分

如果配置文件内容太多,可以拆分成多个配置文件,如:将一个工程的配置文件拆分为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");











一个创业中的苦逼程序员
  • 回复
隐藏