加载配置文件的两种方式
ApplicationContext
是一个接口被翻译为应用上下文其实就是Spring容器, 它的超级父接口BeanFactory
(工厂模式的体现)
// BeanFactory是Spring的超级接口
BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring.xml");
Object vipBean = beanFactory.getBean("vipBean");
System.out.println(vipBean);
配置文件绝对路径的加载方式: 使用FileSystemXmlApplicationContext
类加载配置文件(这种方式较少用,不方便后期的移植)
ApplicationContext applicationContext2 = new FileSystemXmlApplicationContext("d:/spring.xml");
Vip vip = applicationContext2.getBean("vipBean", Vip.class);
System.out.println(vip);
配置文件类路径的加载方式: 使用ClassPathXmlApplicationContext
类加载配置文件(放在resources根目录下相当于放到了类的根路径下,方便后期移植)
// ClassPathXmlApplicationContext是专门从类路径当中加载spring配置文件的一个Spring上下文对象
ApplicationContext applicationContext2 = new ClassPathXmlApplicationContext("spring.xml");
Vip vip = applicationContext2.getBean("vipBean", Vip.class);
System.out.println(vip);
配置文件的名称和个数
通过源码发现ClassPathXmlApplicationContext构造方法的参数上可以传递多个文件路径
public ClassPathXmlApplicationContext(String... configLocations) throws BeansException{
this(configLocations , true , (ApplicationContext)null)
}
第一步: 在类路径下新创建beans.xml和spring.xml文件(spring配置文件的名字可以随意)
// 实体类User
public class User {
}
// 实体类Vip
public class Vip {
}
<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="userBean" class="com.powernode.spring6.bean.User"/>
beans>
<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="vipBean" class="com.powernode.spring6.bean.Vip"/>
beans>
第二步: 测试获取两个配置文件中配置的所有bean
@Test
public void testFirst(){
// 初始化Spring容器上下文解析beans.xml和spring.xml文件,创建所有的bean对象)
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml","spring.xml");
// 根据id获取bean对象
Object userBean = applicationContext.getBean("userBean");
Object vipBean = applicationContext.getBean("vipBean");
//查看获取的bean对象
System.out.println(userBean);
System.out.println(vipBean);
}
配置文件中配置类的类型和id
在spring配置文件中配置的bean可以是任意类(自定义或者第三方的类),只要这个类不是抽象的并且提供了无参数构造方法就可以
<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="userBean" class="com.powernode.spring6.bean.User"/>
<bean id="dateBean" class="java.util.Date"/>
beans>
@Test
public void testFirst(){
// 初始化Spring容器上下文(解析beans.xml文件,创建所有的bean对象)
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
// 根据id获取bean对象
Object userBean = applicationContext.getBean("userBean");
Object dateBean = applicationContext.getBean("dateBean");
System.out.println(userBean);
System.out.println(dateBean);
}
在spring配置文件中配置的bean的id不能重名否则报错
<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="userBean" class="com.powernode.spring6.bean.User"/>
<bean id="userBean" class="com.powernode.spring6.bean.Vip"/>
beans>