带有@Lookup方法的Spring @Bean

问题描述 投票:2回答:2

我已经使用@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();
}
java spring annotations spring-annotations
2个回答
2
投票

由于您自己调用了new,因此该实例不是由Spring创建的,因此不会处理任何Spring注释(包括@Lookup)。您已经注意到,通过将类设为@Component,一切都会按预期工作,因为当您将@Autowire放入另一个类时,Spring会将其实例化。


0
投票

实际上,此行为

© www.soinside.com 2019 - 2024. All rights reserved.