我已经使用@Lookup注释实现了Spring bean。 (此线程很有帮助:How to use spring @Lookup annotation?)
随后,我注意到一种不确定的行为,我不确定这是设计使然还是我自己的误解。 Spring将在使用@ Service,@ Component等注释的ComponentScan版本的bean中实现@Lookup方法,但不会在@Configuration类(Application.java)中定义的@Bean中实现这种方法。
这不是一个大问题,因为我可以从配置中删除@Bean定义,而是直接注释其类;但我想知道是否在某处记录了此行为,或者我执行不正确吗?
@Bean
public Service getService() {
// ServiceImpl has a @Lookup method,
// but Spring does not implement it unless the class itself is annotated.
return new ServiceImpl();
}
由于您自己调用了new
,因此该实例不是由Spring创建的,因此不会处理任何Spring注释(包括@Lookup
)。您已经注意到,通过将类设为@Component
,一切都会按预期工作,因为当您将@Autowire
放入另一个类时,Spring会将其实例化。
实际上,此行为是