如何添加 @Qualifier

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

How to add @Qualifier

问题

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

@Configuration
@Slf4j
@PropertySources(PropertySource("classpath:application.properties"),
                 PropertySource(value = ["file:${credentials.config}"]))
class CredentialsConfig(@Autowired private val env: Environment) {

    @Bean fun getCredentials(): Credentials? {
        val user: String = env.getRequiredProperty("user1")
        val pass: String = env.getRequiredProperty("pass1")
        return Credentials.info(user, pass)
    }

    @Bean fun getCredentials2(): Credentials {
        val user: String = env.getRequiredProperty("user2")
        val pass: String = env.getRequiredProperty("pass2")
        return Credentials.info(user, pass)
    }
}
英文:

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.

@Configuration
@Slf4j
@PropertySources(PropertySource("classpath:application.properties"),
                 PropertySource(value = ["file:${credentials.config}"]))
class CredentialsConfig(@Autowired private val env: Environment) {

    @Bean fun getCredentials(): Credentials? {
        val user: String = env.getRequiredProperty("user1")
        val pass: String = env.getRequiredProperty("pass1")
        return Credentials.info(user, pass)
    }

    @Bean fun getCredentials2(): Credentials {
        val user: String = env.getRequiredProperty("user2")
        val pass: String = env.getRequiredProperty("pass2")
        return Credentials.info(user, pass)
    }
}

答案1

得分: 1

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

@Autowired
@Qualifier("getCredentials")
Credentials credentials;
英文:

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

@Autowired
@Qualifier("getCredentials")
Credentials credentials;

答案2

得分: 1

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

所以...

// 在 CredentialsConfig 中

@Bean("firstCredentials") fun firstCredentials(): Credentials = TODO()
@Bean("secondCredentials") fun secondCredentials(): Credentials = TODO()

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

@Component
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...

// In CredentialsConfig

@Bean("firstCredentials) fun firstCredentials(): Credentials = TODO()
@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):

@Component
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:

确定