Annotation Type Lookup
@Target(METHOD) @Retention(RUNTIME) @Documented public @interface Lookup
An annotation that indicates 'lookup' methods, to be overridden by the container to redirect them back to theBeanFactoryfor agetBeancall. This is essentially an annotation-based version of the XMLlookup-methodattribute, resulting in the same runtime arrangement.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 thegetBeancall for applying them as target factory method arguments or constructor arguments.Such lookup methods can have default (stub) implementations that will simply get replaced by the container, or they can be declared as abstract - for the container to fill them in at runtime. In both cases, the container will generate runtime subclasses of the method's containing class via CGLIB, which is why such lookup methods can only work on beans that the container instantiates through regular constructors: i.e. lookup methods cannot get replaced on beans returned from factory methods where we cannot dynamically provide a subclass for them.
Recommendations for typical Spring configuration scenarios: When a concrete class may be needed in certain scenarios, consider providing stub implementations of your lookup methods. And please remember that lookup methods won't work on beans returned from
@Beanmethods in configuration classes; you'll have to resort to@Inject Provider<TargetBean>or the like instead.- Since:
- 4.1
- Author:
- Juergen Hoeller
- See Also:
BeanFactory.getBean(Class, Object...),BeanFactory.getBean(String, Object...)