5 多配置文件方式
如果在 resources的目录下有多个配置文件,并且都需要加载,有两种配置方式,一种是基于xml的配置方式,一种是基于注解的配置方式。
5.1 基于xml的配置方式
这个有两种方式。
主配置文件中包含其他的配置文件
使用import标签导入
java
<?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="applicationContext2.xml"/>
</beans>
工厂创建的时候直接加载多个配置文件
一般在测试方法里
java
package com.qcby;
import com.qcby.entity.CollectionBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo2 {
@Test
public void run3(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");
CollectionBean collectionBean = (CollectionBean) ac.getBean("collectionBean");
System.out.println(collectionBean);
}
}
5.2 基于注解的配置方式
@Import 注解 Spring 的配置文件可以分成多个配置的,编写多个配置类。用于导入其他配置类。
代码见1.7.4