spring系列(二):springioc:上下文对象ApplicationContext对象的创建、多核心配置文件的方式、ioc对象关联

文章详细介绍了SpringContext上下文对象的两种创建方式:ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,并展示了如何加载多个配置文件。此外,还讨论了对象关联,特别是在Student类中引用Teacher对象,并在配置文件中通过bean的ref属性实现关联。最后,提供了测试代码以验证配置和对象关联的正确性。
摘要由CSDN通过智能技术生成

4 上下文对象的创建

//1 springcontext上下文对象的创建方式1:ClassPathXmlApplicationContext:相对位置是src
ClassPathXmlApplicationContext context1=
    new ClassPathXmlApplicationContext("com/zhiyou100/test01/spring_config.xml");
Student s11=(Student)context1.getBean("stu1");
System.out.println(s11);
context1.close();

//2 springcontext上下文对象的创建方式2:FileSystemXmlApplicationContext:相对位置是项目路径
FileSystemXmlApplicationContext context2=
    new FileSystemXmlApplicationContext("src/com/zhiyou100/test01/spring_config.xml");
Student s12=(Student)context2.getBean("stu1");
System.out.println(s12);
context2.close();

5 多核心配置文件

5.1 方式1:ClassPathXmlApplicationContext构造方法是不定参数

//1 ClassPathXmlApplicationContext构造方法参数是不定参数:可以一次加载多个配置文件
ClassPathXmlApplicationContext context1=
    new ClassPathXmlApplicationContext("com/zhiyou100/test01/spring_config01.xml","com/zhiyou100/test01/spring_config02.xml");
Student s11=(Student)context1.getBean("stu1");
System.out.println(s11);
Student s12=(Student)context1.getBean("stu2");
System.out.println(s12);
context1.close();

5.2 方式2:定义一个总的配置文件:通过import引入其他配置文件

  • spring_config03.xml
<!-- 引入其他配置文件 -->
<import resource="spring_config01.xml"/>
<import resource="spring_config02.xml"/>
  • 创建ClassPathXmlApplicationContext只需要加载总的核心配置文件即可
//2 直接加载总的核心配置文件即可
ClassPathXmlApplicationContext context2=
    new ClassPathXmlApplicationContext("com/zhiyou100/test01/spring_config03.xml");
Student s111=(Student)context2.getBean("stu1");
System.out.println(s111);
Student s122=(Student)context2.getBean("stu2");
System.out.println(s122);
context1.close();

6 对象关联

6.1 创建teacher类

public class Teacher implements Serializable{
	private Integer tid;
	private String tname;
	private String tsex;
    ...
}

6.2 在student类中定义引用指向其关联的teacher对象

public class Student   implements Serializable{
	...
	private Teacher teacher;
    ...
}

6.3 在核心配置文件中为teacher定义bean 并在student的bean中通过ref来关联此对象

<!-- 通过bean标签来创建对象:1 通过property标签给属性赋值 -->
<bean id="stu1" class="com.zhiyou100.test01.Student">  <!-- class指定对象的类型 id指定对象的引用名 -->
    <!-- property标签 通过调用对象的set方法给属性赋值 -->
    <property name="sid" value="1001"/><!-- property标签:给指定的属性赋值 -->
    <property name="sname" value="韩梅梅"/><!-- property标签:给指定的属性赋值 -->
    <property name="sage" value="19"/><!-- property标签:给指定的属性赋值 -->
    <property name="sdy" value="true"/><!-- property标签:给指定的属性赋值 -->
    <property name="score" value="11.6"/><!-- property标签:给指定的属性赋值 -->
    <property name="teacher" ref="tea1"/><!-- 指定当前student的teacher属性的值为id=tea1的bean对象 -->
</bean>
<bean id="tea1" class="com.zhiyou100.test01.Teacher">
    <property name="tid" value="1"/>
    <property name="tname" value="高老师"/>
    <property name="tsex" value=""/>
</bean>
<bean id="stu2" class="com.zhiyou100.test01.Student">  <!-- class指定对象的类型 id指定对象的引用名 -->
    <!-- Integer sid, String sname, Integer sage, Boolean sdy, Float score -->
    <constructor-arg index="0" value="1003"/>
    <constructor-arg index="1" value="韩非子"/>
    <constructor-arg index="2" value="29"/>
    <constructor-arg index="3" value="false"/>
    <constructor-arg index="4" value="35.5"/>
    <constructor-arg index="5" ref="tea1"/>
</bean>

6.4 测试

ClassPathXmlApplicationContext context2=
    new ClassPathXmlApplicationContext("com/zhiyou100/test01/spring_config04.xml");
Student s111=(Student)context2.getBean("stu1");
System.out.println(s111);
Student s1112=(Student)context2.getBean("stu2");
System.out.println(s1112);
context2.close();

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值