Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Spring lookup-method with Spring property

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 ?

like image 823
blue-sky Avatar asked Nov 28 '13 09:11

blue-sky


People also ask

What is lookup method injection in Spring?

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.

What is the use of @lookup annotation?

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.

What is the return type of lookup () method?

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.

How can you solve the problem of lookup method injection?

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.


2 Answers

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>
like image 183
Debojit Saikia Avatar answered Oct 13 '22 08:10

Debojit Saikia


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/

like image 2
techiesantosh Avatar answered Oct 13 '22 08:10

techiesantosh