I've implemented a Spring bean utilizing the @Lookup annotation. (This thread was helpful: How to use spring @Lookup annotation?)
I've subsequently noticed a strange behavior that I'm not sure is by design or my own misunderstanding. Spring will implement a @Lookup method in a ComponentScan-ed bean annotated with @Service, @Component, etc. but will not implement such a method in a @Bean defined in a @Configuration class (Application.java).
This isn't a big problem, as I can remove the @Bean definition from the configuration and instead annotate its class directly; but I'm wondering whether this behavior is documented somewhere, or am I implementing it incorrectly?
@Bean
public Service getService() {
// ServiceImpl has a @Lookup method,
// but Spring does not implement it unless the class itself is annotated.
return new ServiceImpl();
}
In fact this behavior is a limitation of the @Lookup
annotation. The latest version of Spring documentation describes the caveat more explicitly than it did back in version 4.1.
...please remember that lookup methods won't work on beans returned from
@Bean
methods in configuration classes;
In general, objects returned from @Bean
methods do have their annotations processed; @Lookup
is an exception to the typical behavior.