Problem:
When injecting a multi-instance bean into a singleton bean, it is not getting to that multi-sample object, because, when the singleton is initialized, it is initialized directly, and this multi-instance bean is always getting the first initialized bean.
Configuration file:
<?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= "Dong Dong" ></property> <property name= "password" value= "123" ></property> <!--<aop:scoped-proxy/> -- </bean> </beans> |
Singletonobject object:
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 object:
package Com.lzd.spring.context.resource.test.bean; /** * * <p>description: This is a multi-instance object </p> * @ClassName: Prototypeobject * @author lzd February 15, 2017 morning 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 Test class:
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 <; i++) System.out.println (S.getprototype ());
}
}
|
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/8D/9D/wKiom1ijypGAyh57AAIS3UbJ9ZM796.png "title=" 1.png "alt=" Wkiom1ijypgayh57aais3ubj9zm796.png "/>
Problem solving:
Configuration file:
<?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= "Dong Dong" ></property>
<property name= "password" value= "123" ></property> <!--Add the following, one row, each created, is a proxy object, and then the injection gets all the new objects --
<aop:scoped-proxy/> </bean> </beans> |
|
Test results:
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/8D/9D/wKiom1ijy_TxHrPNAAG0x6YhEuw734.png "title=" 1.png "alt=" Wkiom1ijy_txhrpnaag0x6yheuw734.png "/>
Multiple bean injection in spring singleton bean