7 lookup-method of singleton injection into multiple instances
Spring
There is a mechanism to dynamically implement or override the specified method specified in the bean
container bean
, and then specify the return value as bean
another in the container bean
. Now let's talk about how to use this mechanism for the application scenario mentioned above where a single instance bean
needs to be injected into multiple instances . bean
Suppose we have a singleton that beanA
needs to inject a multi-instance BeanB
type bean
, then we can do this.
1. beanA
Define a method in the corresponding class to obtain BeanB
it. It is enough to have such a method, which can be an empty implementation. At that time, it will Spring
be rewritten or implemented for us, and then a BeanB
type will be returned bean
. To do this, we can define our BeanA
definition as follows:
public class BeanA {
/* *
* Need to use the method of BeanB
*/ public void doSomething () {
BeanB beanB = this . getBeanB();
System . out . println(beanB);
// ...
}
/* *
* Define a method with a return value of BeanB to get a bean of type BeanB, this method will be overridden by Spring.
* @return
*/ public BeanB getBeanB () {
return null ;
}
}
2. ApplicationContext
Define a single instance beanA
and a multiple instance beanB
in , and the method lookup-method
specified by the element will be overridden and returned to the container .beanA
getBeanB
Spring
bean
beanB
<bean id="beanB" class="com.app.BeanB" scope="prototype"/>
< bean id = " beanA " class = " com.app.BeanA " >
<!-- Indicates that the getBeanB() method will be rewritten by Spring and return a bean named beanB -->
< lookup-method name = " getBeanB " bean = " beanB " />
</bean>
After the above two steps, each time the scheduling beanA
method getBeanB()
is used, one will Spring
be obtained from the bean
container again beanB
. Because it is defined as a multi-instance form, a new object beanB
will be obtained every time .BeanB
After one is bean
specified lookup-method
, a subclass of the corresponding type Spring
will be CGLIB
dynamically generated , and then the method specified by the element bean
will be implemented or rewritten in the subclass, and the specified element will be obtained from the container as the return value for return. When the specified type is an abstract class and the specified method is an abstract method, the abstract method will be implemented, otherwise it will be overridden. The real type defined is the type of the dynamically generated class, but it can be used as the type specified by itself, because the dynamically generated class is inherited from the type specified by itself.lookup-method
bean
lookup-method
bean
bean
lookup-method
Spring
lookup-method
bean
Spring
bean
bean
Since it Spring
is necessary to dynamically generate a class to override or implement the specified method, we must ensure that the lookup-method
specified method can be overridden, which requires that the method is accessible to subclasses and cannot be of final
type. Specifically lookup-method
, the method specified by the element needs to have the following form:
<public|protected> [abstract] <return-type> methodName(no-arguments)
As you can see, lookup-method
the specified method cannot have parameters yet.
(Note: This article is written based on Spring 4.1.0)