使用@Lookup方法的Spring @Bean [英] Spring @Bean with @Lookup method

查看:248
本文介绍了使用@Lookup方法的Spring @Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用@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版本的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的最新版本

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方法 do 返回的对象将对其注释进行处理. @Lookup是典型行为的例外.

In general, objects returned from @Bean methods do have their annotations processed; @Lookup is an exception to the typical behavior.

这篇关于使用@Lookup方法的Spring @Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆