简体   繁体   English

Spring 中使用 @Bean 和 @Qualifier 注释的 Bean 名称解析

[英]Bean name resolution in Spring using @Bean and @Qualifier annotations

I just found a behaviour of Spring that I cannot understand.我刚刚发现了一种我无法理解的 Spring 行为。 I am using Spring Boot 1.5.x.我正在使用 Spring Boot 1.5.x。

In a configuration class, I declared two different beans.在一个配置类中,我声明了两个不同的 bean。

@Configuration
class Config {
    @Bean("regularRestTemplate")
    public RestTemplate regularRestTemplate(String toLog) {
        return new RestTemplateBuilder().setConnectTimeout(100)
                                        .setReadTimeout(100)
                                        .additionalInterceptors((request, body, execution) -> {
                                            log.info("Inside the interceptor {}", toLog);
                                            return execution.execute(request, body);
                                        })
                                        .build();
    }
    @Bean("exceptionalRestTemplate")
    public RestTemplate regularRestTemplate() {
        return new RestTemplateBuilder().setConnectTimeout(100)
                                        .setReadTimeout(100)
                                        .build()
    }
}

Then, I have a class that should use the bean called exceptionalRestTemplate .然后,我有一个类应该使用名为exceptionalRestTemplate的 bean。

@Component
class Client {
    private RestTemplate restTemplate;
    @Autowired
    public Client(@Qualifier("exceptionalRestTemplate") RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
    // Code that uses the injected rest template
}

Since I specified the name of the bean I want to be injected using the @Qualifier annotation, I would expect that Spring injects the bean called exceptionalRestTemplate .由于我使用@Qualifier注释指定了要注入的 bean 的名称,因此我希望 Spring 注入名为exceptionalRestTemplate的 bean。 However, the bean called regularRestTemplate is actually used during the injection process.但是,在注入过程中实际上使用了名为regularRestTemplate的bean。

It turns out that the problem was in the name of the methods that declare the beans in the configuration class.事实证明,问题在于在配置类中声明 bean 的方法的名称。 Both are colled regularRestTemplate .两者都归类为regularRestTemplate Changing the second method name, solve the problem.更改第二个方法名,解决问题。

My question is, why?我的问题是,为什么? I know that Spring uses the names of classes and methods annotated with the @Bean or with the @Component , @Service , etc... annotations to give a name to Java object inside the resolution map.我知道 Spring 使用用@Bean@Component@Service等注释的类和方法的名称...注释来为解析映射中的 Java 对象命名。 But, I supposed that giving a name inside these annotations would override this behaviour.但是,我认为在这些注释中命名会覆盖这种行为。

Does anybody tell me what's going on?有人告诉我这是怎么回事吗?

Bean qualifier and bean name are different meanings. bean 限定符和 bean 名称是不同的含义。 You qualified new bean but tried to override it (arguments don't matter).您对 new bean 进行了限定,但试图覆盖它(参数无关紧要)。 In your application, you cannot override beans so you have the only first one.在您的应用程序中,您不能覆盖 bean,因此您只有第一个 bean。

You can check this 'theory'.你可以检查这个“理论”。 Add a parameter in your configuration在您的配置中添加一个参数

spring.main.allow-bean-definition-overriding=true

and start your application again.并再次启动您的应用程序。 After that, you will have only a second bean.在那之后,您将只有第二个豆子。

This is an explanation of the collision.这是对碰撞的解释。 But the solution is a separation of beans to different configurations.但解决方案是将 bean 分离为不同的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring @Bean 名称,不适用于 @Qualifier - Spring @Bean Name, is not working with @Qualifier 使用Java Spring中的注释将数据库列名称映射到Bean属性 - Mapping a DB column name to a Bean property using annotations in Java Spring 是否可以在 Spring Framework 中使用注释设置 bean 名称? - Is it possible to set a bean name using annotations in Spring Framework? 如何在Spring中的运行时设置bean限定符名称 - How to set a bean qualifier name at run time in Spring 如何使用注解Bean和ComponentScan正确创建Bean Spring? - How to proper create bean Spring using annotations Bean and ComponentScan? 使用注释时的Spring Mvc DataSource Bean - Spring Mvc DataSource Bean when using annotations 如何在spring中通过默认的限定符bean名称获取@Service bean - how do i get @Service bean by its default qualifier bean name in spring 使用@Qualifier批注和在Spring中直接注入bean有什么区别? - What is the difference between using @Qualifier annotation and directly injecting bean in spring? 使用@Qualifier 指定的 bean 注入点时,如何扩展命名的 Spring bean? - How do I extend a named Spring bean when using a @Qualifier specified bean injection point? Spring - 在运行时使用自定义限定符获取 bean - Spring - getting bean at runtime with custom qualifier
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM