In spring, when a singleton bean relies on a prototype bean, because the singleton Bean is Singleton, the prototype Bean becomes a singleton in the singleton Bean.
How does this work out??? You can use the Lookup-method provided by spring to inject.
Example: List related classes First: the instructions in the Code are sufficient to illustrate the problem. User class: Prototype bean
Package Com.cn.pojo;import Java.io.serializable;public Class User implements serializable{/** * */private static Final long serialversionuid = 1l;private int userid;private string username;private string Userpwd;private string UserInfo ;p ublic int getUserId () {return userId;} public void Setuserid (int userId) {this.userid = userId;} Public String GetUserName () {return userName;} public void Setusername (String userName) {this.username = UserName;} Public String getuserpwd () {return userpwd;} public void Setuserpwd (String userpwd) {this.userpwd = userpwd;} Public String GetUserInfo () {return userInfo;} public void Setuserinfo (String userInfo) {this.userinfo = UserInfo;} }
Lookupmethod class: Singleton bean
Package Com.cn.springtest;import com.cn.pojo.user;/** * Lookupmethod for Singleton,user for Propotype * when Singleton The bean relies on the propotype bean, which can be used to add Look-method to the configuration bean to resolve the * @author Administrator * */public class Lookupmethod {Private user User ;p ublic User GetUser () {return user;} public void SetUser (user user) {this.user = user;}}
Core configuration file:
<!--user Userbean--><bean id = "User" class= "Com.cn.pojo.User" scope= "prototype" > <property name= " UserId "value=" 1 "/> <property name=" UserName "value=" UserName "/> <property name=" Userpwd "value = "Userpwd"/> <property name= "UserInfo" value= "UserInfo"/></bean><!--lookupmethod Lookupmethod is a singleton, user is the prototype acquisition instance--><bean ID = "Lookupmethod" class= "Com.cn.springTest.LookupMethod" ><!-- <property name= "user" ref= "user"/>- <!--This will take the user to a single case to get the <lookup-method name= " GetUser "bean=" user "/><!--Lookup-method method will take user in front of the prototype to get--></bean>
Test class: Springlookupmethod
Import Com.cn.util.springcontextutil;public class Springlookupmethod {public static void main (string[] args) { ApplicationContext actx = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); Actx.getbean (" Springcontextutil "); Lookupmethod Lookupmethod = (lookupmethod) springcontextutil.getbean ("Lookupmethod"); System.out.println (Lookupmethod); Lookupmethod lookupMethod1 = (lookupmethod) springcontextutil.getbean ("Lookupmethod"); System.out.println (LOOKUPMETHOD1); System.out.println (Lookupmethod.getuser ()); System.out.println (Lookupmethod1.getuser ()); System.out.println (Lookupmethod.getuser ()); System.out.println (Lookupmethod1.getuser ());}}
Such tests are sufficient to illustrate the problem.
When the singleton Bean relies on the propotype bean, it can be used to add Look-method to the configuration bean to resolve