[英]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.