单例bean引用原型bean
场景:单例bean引用不变的原型对象
省略 headMaster、teacher、student三个属性
xml文件内容
定义headMaster和student对原型bean,teacher为单例bean。其中teacher中注入headMaster和student属性。
<?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="headMaster" class="com.spring.learn.common.staff.HeadMaster" scope="prototype"/>
<bean id="teacher" class="com.spring.learn.common.staff.Teacher">
<property name="headMaster" ref="headMaster"/>
<property name="student" ref="student"/>
</bean>
<bean id="student" class="com.spring.learn.common.staff.Student" scope="prototype"/>
</beans>
测试和结果
单例bean的teacher中,headMaster和student属性始终不变。从applicationContext中重新获取对应bean后,返回新建对象。<