Spring (07) - lookup-method of singleton injection into multiple instances

7 lookup-method of singleton injection into multiple instances

SpringThere is a mechanism to dynamically implement or override the specified method specified in the beancontainer bean, and then specify the return value as beananother in the container bean. Now let's talk about how to use this mechanism for the application scenario mentioned above where a single instance beanneeds to be injected into multiple instances . beanSuppose we have a singleton that beanAneeds to inject a multi-instance BeanBtype bean, then we can do this.

1. beanADefine a method in the corresponding class to obtain BeanBit. It is enough to have such a method, which can be an empty implementation. At that time, it will Springbe rewritten or implemented for us, and then a BeanBtype will be returned bean. To do this, we can define our BeanAdefinition 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. ApplicationContextDefine a single instance beanAand a multiple instance beanBin , and the method lookup-methodspecified by the element will be overridden and returned to the container .beanAgetBeanBSpringbeanbeanB

	<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 beanAmethod getBeanB()is used, one will Springbe obtained from the beancontainer again beanB. Because it is defined as a multi-instance form, a new object beanBwill be obtained every time .BeanB

After one is beanspecified lookup-method, a subclass of the corresponding type Springwill be CGLIBdynamically generated , and then the method specified by the element beanwill 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-methodbeanlookup-methodbeanbeanlookup-methodSpringlookup-methodbeanSpringbeanbean

Since it Springis necessary to dynamically generate a class to override or implement the specified method, we must ensure that the lookup-methodspecified method can be overridden, which requires that the method is accessible to subclasses and cannot be of finaltype. 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-methodthe specified method cannot have parameters yet.

(Note: This article is written based on Spring 4.1.0)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326449368&siteId=291194637