問題:
當在一個單例Bean中注入一個多例Bean的時候,是獲取不到那個多例對象的,因為,單例在初始化的時候,就直接初始化,這個多例Bean啦, 一直獲取的是第一次初始化的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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="single" class="com.lzd.spring.context.resource.test.bean.SingletonObject">
<property name="prototypeObject" ref="prototype"></property>
</bean>
<bean id="prototype" class="com.lzd.spring.context.resource.test.bean.PrototypeObject" scope="prototype">
<property name="name" value="劉東"></property>
<property name="password" value="123"></property>
<!-- <aop:scoped-proxy/> -->
</bean>
</beans>
SingletonObject對象:
package com.lzd.spring.context.resource.test.bean;
public class SingletonObject {
private PrototypeObject prototype;
public void setPrototypeObject(PrototypeObject prototype){
this.prototype = prototype;
}
public PrototypeObject getPrototype(){
return this.prototype;
}
}
PrototypeObject對象:
package com.lzd.spring.context.resource.test.bean;
/**
*
* <P>Description: 這是一個多例的對象</P>
* @ClassName: PrototypeObject
* @author lzd 2017年2月15日 上午10:54:52
*/
public class PrototypeObject {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
ApplicationResourceTest測試類:
package com.lzd.spring.context.resource.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lzd.spring.context.resource.test.bean.SingletonObject;
public class ApplicationResourceTest {
@Test
public void applicationResource(){
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:application.xml"});
SingletonObject s = (SingletonObject) context.getBean("single");
for (int i = 0; i < 10; i++)
System.out.println(s.getPrototype());
}
}
問題解決:
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="single" class="com.lzd.spring.context.resource.test.bean.SingletonObject">
<property name="prototypeObject" ref="prototype"></property>
</bean>
<bean id="prototype" class="com.lzd.spring.context.resource.test.bean.PrototypeObject" scope="prototype">
<property name="name" value="劉東"></property>
<property name="password" value="123"></property>
<!-- 添加下面,一行,每次創建的,都是一個代理對象,然後注入獲取的都是新的對象 -->
<aop:scoped-proxy/>
</bean>
</beans>
測試結果: