Zen Building Moon (Http://www.cnblogs.com/yaoyinglong)
We usually use the lookup method injection, which allows spring to replace the abstract or concrete method of a bean, returning the result of the other bean in the lookup container, which is usually a non-singleton bean.
The scope of a stateless bean can generally be configured as a singleton single instance, if we inject prototype BEANB into the beana of Singleton, and want to call Beana getbeanb () each time Can return a new BEANB, this requirement cannot be achieved using traditional injection methods . Because singleton beans inject The associated bean only once, although the scope of the BEANB is the prototype type, the returned object is not the bean that was injected the first time.
If you want to return a new BEANB each time you call Beana's GETBEANB (), an alternative is to have the bean implement the Beanfactoryaware interface, allowing Beana to access the container's references, and the following code can be implemented in a way that achieves the purpose:
[+]View Code
Public class bean_a implements beanfactoryaware{
Private bean_b bean_b=
null;
Private beanfactory beanfactory=
null; @Override Public
void setbeanfactory (beanfactory beanfactory)
throws beansexception { This
. beanfactory=beanfactory; } Public
bean_b getbean_b () {
return beanfactory. Getbean ("Bean_b", Bean_b.
class); }}
Recall the lifetime of the Bean in the first story, when a bean's Pojo class implements the Beanfactoryaware interface, when the bean instantiation is complete and the Setter Method drop property is set, The Setbeanfactory method of the Beanfactoryaware interface is called so that we can get a reference to the beanfactory. With Beanfactory we can get a completely new bean instance of scope="prototype" .
However, this method makes a pure Pojo class contaminated, so this method is not normally used.
1. Using the lookup method to inject
The spring IOC container has the ability to replicate bean methods , thanks to the Cglib class package, where cglib can manipulate the class bytecode dynamically at run time, allowing spring to replace the abstract or concrete method of a bean, Now we use the Lookup method injection to implement the above function:
[+]View Code
Public class bean_a{
Private bean_b bean_b=
null; Public
bean_b getbean_b () {
return bean_b; } Public
void setbean_b (Bean_b bean_b) { This
. Bean_b = Bean_b; }}
Note that our bean_a here is a pure Pojo class. Then add the lookup method injection to Bean_a in the spring configuration file so that spring replaces the Getbean_b () method.
[+]View Code
<Bean id="Bean_b" class="Smzq". Bean_b " Scope=" prototype "/>
<bean id=
"bean_a" class=
"Smzq". Bean_a "> <lookup-method name=
"Getbean_b " Bean=
"bean_b"/> </Bean>
Test:
Done.
Note: The lookup method can also create subclasses or implementation classes for the bean dynamically, and now we declare an interface as follows:
[+]View Code
Public Interface beaninterface {
Bean_b Getbean_b ();}
It is also configured in the spring configuration file, but this time the configuration is not a class, but an interface. It's amazing!
[+]View Code
<Bean id="Bean_b" class="Smzq". Bean_b " scope=" prototype "/>
<bean id=
"Beaninter" class=
"Smzq". Beaninterface "> <lookup-method name=
"Getbean_b" Bean =
"bean_b"/> </Bean>
Test again:
This shows that spring does create an implementation class for our Baseinter interface. So this is an abstract class? Spring also creates a subclass for it.
<lookup-method> An element has only two attributes:
Name: Specifies the name of the method to let spring implement or replace, which returns a Bean object.
Bean: Specifies the return value of the method specified by the Name property.
Spring Application Tutorial-2 method injection