@Lookup
tells Spring to return an instance of the method's return type when it gets invoked. In this case Spring will override the annotated method and will use the method's return type and parameters as arguments to the call to BeanFactory.getBean()
.An annotation that indicates 'lookup' methods, to be overridden by the container to redirect them back to the BeanFactory for a getBean call.
The resolution of the target bean can either be based on the return type (getBean(Class)) or on a suggested bean name (getBean(String)),
in both cases passing the method's arguments to the getBean call for applying them as target factory method arguments or constructor arguments.
@Lookup
is useful for:lookup-method
in applicationContext.xml.@Configuration
public class AppConfig {
@Bean
@Scope("prototype")
public PrototypeBean prototypeBean() {
return new PrototypeBean();
}
@Bean
public SingletonBean singletonBean() {
return new SingletonBean();
}
}
prototype
scope, the other one is a singleton
(by default).public class SingletonBean {
//... member variables, etc.
@Autowired
private PrototypeBean prototypeBean;
public SingletonBean() {
logger.info("Singleton instance created");
}
public PrototypeBean getPrototypeBean() {
logger.info(String.valueOf(LocalTime.now()));
return prototypeBean;
}
//... getters and setters
}
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext context
= new AnnotationConfigApplicationContext(AppConfig.class);
SingletonBean firstSingleton = context.getBean(SingletonBean.class);
PrototypeBean firstPrototype = firstSingleton.getPrototypeBean();
// get singleton bean instance one more time
SingletonBean secondSingleton = context.getBean(SingletonBean.class);
PrototypeBean secondPrototype = secondSingleton.getPrototypeBean();
isTrue(firstPrototype.equals(secondPrototype), "The same instance should be returned");
}
Singleton Bean created
Prototype Bean created
11:06:57.894
// should create another prototype bean instance here
11:06:58.895
getPrototypeBean()
method is called, a new instance of PrototypeBean will be returned from the ApplicationContext.@Component
public class SingletonLookupBean {
@Lookup
public PrototypeBean getPrototypeBean() {
return null; // Stub - Method will be overridden
}
}
lookup-method
in application.xml would look something like this:<bean id="prototypeBean" class="net.netconomy.uselookup.PrototypeBean" scope="prototype"/>
<bean id="singletonLookupBean" class="net.netconomy.uselookup.SingletonLookupBean">
<lookup-method name="getPrototypeBean" bean="prototypeBean"/>
</bean>
@Test
public void whenLookupMethodCalled_thenNewInstanceReturned() {
// ... initialize context
SingletonLookupBean first = this.context.getBean(SingletonLookupBean.class);
SingletonLookupBean second = this.context.getBean(SingletonLookupBean.class);
assertEquals(first, second);
assertNotEquals(first.getPrototypeBean(), second.getPrototypeBean());
}
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |