如何添加 @Qualifier

huangapple go评论103阅读模式
英文:

How to add @Qualifier

问题

怎样在这两个Bean之间添加限定符以进行区分?我知道我需要使用@Qualifier注解,但不太确定如何将其添加到Beans中,然后如何创建带有对适当的Bean引用的自动连接对象。

  1. @Configuration
  2. @Slf4j
  3. @PropertySources(PropertySource("classpath:application.properties"),
  4. PropertySource(value = ["file:${credentials.config}"]))
  5. class CredentialsConfig(@Autowired private val env: Environment) {
  6. @Bean fun getCredentials(): Credentials? {
  7. val user: String = env.getRequiredProperty("user1")
  8. val pass: String = env.getRequiredProperty("pass1")
  9. return Credentials.info(user, pass)
  10. }
  11. @Bean fun getCredentials2(): Credentials {
  12. val user: String = env.getRequiredProperty("user2")
  13. val pass: String = env.getRequiredProperty("pass2")
  14. return Credentials.info(user, pass)
  15. }
  16. }
英文:

How can I add a qualifier to distinguish between these two beans? I know I need to use the @Qualifier annotation but I am not sure how to add it in the beans and then how to create the autowired object with reference to the appropriate bean.

  1. @Configuration
  2. @Slf4j
  3. @PropertySources(PropertySource("classpath:application.properties"),
  4. PropertySource(value = ["file:${credentials.config}"]))
  5. class CredentialsConfig(@Autowired private val env: Environment) {
  6. @Bean fun getCredentials(): Credentials? {
  7. val user: String = env.getRequiredProperty("user1")
  8. val pass: String = env.getRequiredProperty("pass1")
  9. return Credentials.info(user, pass)
  10. }
  11. @Bean fun getCredentials2(): Credentials {
  12. val user: String = env.getRequiredProperty("user2")
  13. val pass: String = env.getRequiredProperty("pass2")
  14. return Credentials.info(user, pass)
  15. }
  16. }

答案1

得分: 1

你可以在进行Credentials的自动装配(Autowire)时,只需使用@Bean的名称添加@Qualifier。

  1. @Autowired
  2. @Qualifier("getCredentials")
  3. Credentials credentials;
英文:

You could just add @Qualifier with bean name whenever you do an Autowire of Credentials.

  1. @Autowired
  2. @Qualifier("getCredentials")
  3. Credentials credentials;

答案2

得分: 1

在这种情况下,我发现明确为我的 beans 指定名称是有益的,这样更清楚地知道我在选择哪个。否则,你最终会得到 Spring 决定的名称(基于方法名)。当我们想要注入一个 bean,但存在多个相同类型的 bean 时,我们在注入点使用 @Qualifier 注解,指定我们关心的 bean 的名称。

所以...

  1. // 在 CredentialsConfig 中
  2. @Bean("firstCredentials") fun firstCredentials(): Credentials = TODO()
  3. @Bean("secondCredentials") fun secondCredentials(): Credentials = TODO()

当在这些中的一个中进行连接时,你可以添加一个 @Qualifier 来选择你想要的特定实现(注意,如果你使用构造函数注入,则不需要 @Autowired):

  1. @Component
  2. class MyComponent(@Qualifier("firstCredentials") creds: Credentials) { ... }
英文:

In situations like this, I find it beneficial to explicitly name my beans so it is more clear which one I am picking. Otherwise, you will end up with what Spring decides to call it (based on the method name). When we want to inject a bean, but there are more than one of them, we use the @Qualifer annotation at the injection point, specifying the name of the bean we care about.

So...

  1. // In CredentialsConfig
  2. @Bean("firstCredentials) fun firstCredentials(): Credentials = TODO()
  3. @Bean("secondCredentials) fun secondCredentials(): Credentials = TODO()

And when wiring in one of these, you can add a @Qualifier to pick your specific implementation (note, if you use constructor injection, you don't need @Autowired):

  1. @Component
  2. class MyComponent(@Qualifier("firstCredentials") creds: Credentials) { ... }

huangapple
  • 本文由 发表于 2020年10月19日 22:04:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64429022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定