04Spring的核心配置文件

核心配置文件

加载配置文件的两种方式

ApplicationContext是一个接口被翻译为应用上下文其实就是Spring容器, 它的超级父接口BeanFactory(工厂模式的体现)

  • FileSystemXmlApplicationContext,ClassPathXmlApplicationContext都是ApplicationContext的实现类
// 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 {
}
<?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,这样spring才可以帮助我们管理这个对象-->
    <bean id="userBean" class="com.powernode.spring6.bean.User"/>
</beans>
<?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,这样spring才可以帮助我们管理这个对象-->
    <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可以是任意类(自定义或者第三方的类),只要这个类不是抽象的并且提供了无参数构造方法就可以

<?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="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不能重名否则报错

在这里插入图片描述

<?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重复会报错-->
    <bean id="userBean" class="com.powernode.spring6.bean.User"/>
    <bean id="userBean" class="com.powernode.spring6.bean.Vip"/>
</beans>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值