Spring @Bean 与 @Lookup 方法 [英] Spring @Bean with @Lookup method
问题描述
我已经使用 @Lookup 注释实现了一个 Spring bean.(此线程很有帮助:如何使用 spring @Lookup 注释?)
I've implemented a Spring bean utilizing the @Lookup annotation. (This thread was helpful: How to use spring @Lookup annotation?)
我随后注意到一种奇怪的行为,我不确定是有意为之还是我自己的误解.Spring 将在使用@Service、@Component 等注解的 ComponentScan ed bean 中实现 @Lookup 方法,但不会在 @Configuration 类 (Application.java) 中定义的 @Bean 中实现此类方法.
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).
这不是什么大问题,因为我可以从配置中删除@Bean 定义,而是直接注释它的类;但我想知道这种行为是否在某处记录在案,或者我是否错误地实施了它?
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();
}
推荐答案
实际上这种行为是@Lookup
注释的限制.Spring最新版本 文档 比 4.1 版中更明确地描述了警告.
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.
...请记住,查找方法不适用于从配置类中的 @Bean
方法返回的 bean;
...please remember that lookup methods won't work on beans returned from
@Bean
methods in configuration classes;
一般来说,从@Bean
方法返回的对象确实会处理它们的注释;@Lookup
是典型行为的例外.
In general, objects returned from @Bean
methods do have their annotations processed; @Lookup
is an exception to the typical behavior.
这篇关于Spring @Bean 与 @Lookup 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!