【问题标题】:Spring @Bean with @Lookup method使用 @Lookup 方法的 Spring @Bean
【发布时间】:2015-05-28 04:43:49
【问题描述】:

我已经使用 @Lookup 注释实现了一个 Spring bean。 (这个帖子很有帮助:How to use spring @Lookup annotation?

我随后注意到一种奇怪的行为,我不确定是设计使然还是我自己的误解。 Spring 将在使用 @Service、@Component 等注释的 ComponentScan-ed 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


    【解决方案1】:

    事实上,这种行为 @Lookup 注释的限制。最新版本的 Spring documentation 比 4.1 版更明确地描述了警告。

    ...请记住,查找方法不适用于从配置类中的 @Bean 方法返回的 bean;

    一般来说,从@Bean 方法返回的对象确实会处理它们的注释; @Lookup 是典型行为的一个例外。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 2011-04-22
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      相关资源
      最近更新 更多