Lookup method Injection

Source: Internet
Author: User

Lookup method injection means that the container can override the bean abstraction or specific method in the container and return the result of searching for other beans in the container. The bean to be searched is generally a non-singleton bean in the scenario described above (although it can also be a singleton ). Spring uses the CGLIB library to modify the binary code above the client class to meet the preceding scenario requirements.

The client class that contains method injection must define the method in the following form:

Protected abstract SingleShotHelper createSingleShotHelper ();
If the method is not abstract, Spring will directly rewrite the existing implementation. In the case of XmlBeanFactory, you can use the lookup-method attribute in the bean definition to instruct Spring to inject/override this method so as to return a specific bean from the container. For example:

<! -- A stateful bean deployed as a protype (non-singleton) -->
<Bean id = "singleShotHelper class ="... "singleton =" false ">
</Bean>

<! -- MyBean uses singleShotHelper -->
<Bean id = "myBean" class = "...">
<Lookup-method name = "createSingleShotHelper"
Bean = "singleShotHelper"/>
<Property>
...
</Property>
</Bean>
When myBean needs a new singleShotHelper instance, it will call its own createSingleShotHelper method. It is worth noting that beans deployment personnel must carefully deploy singleShotHelper as a non-singleton deployment (if necessary ). If it is deployed as a singleton (the default is singletion unless explicitly stated), the same singleShotHelper instance will be returned each time.

Note that Lookup injection can be combined with constructor injection (providing optional constructor parameters for created beans) or with setter injection (setting properties on created beans ).

Java code
Public class UserDao {

Public UserDao (){}

Private String name = "";
 
Public UserDao (String name ){
This. name = name;
}
 
Public void create (){
System. out. println ("create user from-" + name );
}
}
 
Public abstract class UserManager {
Public UserDao getUserDao (){
Return new UserDao ("UserManager. getUserDao ()");
}
 
Public void createUserDao (){
UserDao dao = getUserDao ();
Dao. create ();
}
// Method required for method injection, implemented by spring
Public abstract UserDao getSingleUserDao ();

}

Xml Code
<Bean name = "userManager" class = "com. yoyousfot. spring. lookup. UserManager">
<Lookup-method name = "getSingleUserDao" bean = "userDao"/>
</Bean>
<Bean name = "userDao" class = "com. yoyousfot. spring. lookup. UserDao">
<Constructor-arg>
<Value> lookup method </value>
</Constructor-arg>
</Bean>


With this mechanism, we can easily replace UserDao with other types of compatible objects without modifying the original system code, without affecting the original system. Spring uses CGLIB to dynamically implement the sub-class of userManager at the bytecode level, and rewrite the getUserDao method to implement this magical function.

Author: "Jelly_JAVA blog"
 

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.