When the singleton Bean relies on the propotype bean, it can be used to add Look-method to the configuration bean to resolve

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.