I'm trying to inject a property everytime a bean (myBean) is called using a lookup method and Spring dependency injection :
<bean id="myBean" class="com.myclass"
<property name="default" ref="myDefault" >
<lookup-method name="getUri" bean="defaultUri" />
</property>
</bean>
<bean id="defaultUri" scope="prototype" class="DefaultUri" >
</bean>
class myclass {
public String getUri(){
return "test"
}
}
Above XML returns this error on startup :
"XML document from PortletContext resource is invalied"
The error seems to be because <lookup-method name="getUri" bean="defaultUri" />
is configured incorrectly.
How can I configure a Spring lookup method within a String 'property' as I'm trying to implement in above XML ?
To put it in simple words, lookup method injection is the process to override a Spring bean at the runtime. The constructor and property are the most common used dependency injection methods. Both the options happen during the initialization of the Bean.
2. Why @Lookup? A method annotated with @Lookup tells Spring to return an instance of the method's return type when we invoke it. Essentially, Spring will override our annotated method and use our method's return type and parameters as arguments to BeanFactory#getBean.
The parameter types of the method handle will be those of the constructor, while the return type will be a reference to the constructor's class. The constructor and all its argument types must be accessible to the lookup class.
Spring Batch It is solution of above problem in injecting different scoped beans. It works as that since singleton beans are instantiated at context creation, but it changes the way prototype-scoped are handled, from injection to created by an abstract method.
Lookup method injection is the ability of the container to override methods on container managed beans, to return the lookup result for another named bean in the container.
Now, suppose you want to get a new instance of DefaultUri
(which is a prototype bean) every time you call a method (let it be createDefaultUri
) in myclass
(which is a singleton bean). Then you can define MyClass
as this:
class abstract Myclass {
public String getUri(){
// create a new instance of DefaultUri
DefaultUri defaultUri = createDefaultUri();
return "test"
}
protected abstract DefaultUri createDefaultUri();
}
The Spring Framework will generate a dynamic subclass of Myclass
that will override the createDefaultUri
method to provide a new instance of DefaultUri
every time it is requested for.
You can now define the name of lookup-method
name in the Myclass
bean definition as this:
<bean id="defaultUri" scope="prototype" class="DefaultUri">
</bean>
<bean id="myBean" class="com.myclass"
<lookup-method name="createDefaultUri" bean="defaultUri" />
</bean>
Suppose singleton bean A needs to use non-singleton (prototype) bean B, perhaps on each method invocation on A(getBeanB()), we expect to get new instance of bean B for every request. But The container only creates the singleton bean A once, and thus only gets one opportunity to set the properties. The container cannot provide bean A with a new instance of bean B every time one is needed. To get new new instance of bean B for every request we need to use lookup-method injection
Please refer to http://www.javapointer.com/spring/spring-core/spring-lookup-method-injection/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With