首页 > 解决方案 > Spring - 在运行时使用自定义限定符获取 bean

问题描述

我创建了一个自定义 Spring@Qualifier注释:

@Target({
        ElementType.FIELD,
        ElementType.METHOD,
        ElementType.PARAMETER, 
        ElementType.TYPE,
        ElementType.ANNOTATION_TYPE
})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Database {
    String value() default "";
}

然后我将此注释应用于各种实现 Bean:

@Repository
@Database("mysql")
class MySqlActionRepository implements ActionRepository {}

@Repository
@Database("oracle")
class OracleActionRepository implements ActionRepository {}

@Repository
@Database("sqlserver")
class SqlServerActionRepository implements ActionRepository {}

现在,在运行时,只有这些 Bean 中的一个必须可用于注入,我创建了一个@PrimaryBean 方法。

@Bean
@Primary
ActionRepository actionRepository(
        final ApplicationContext applicationContext,
        final Configuration configuration) {
    final var database = configuration.getString("...");
    return BeanFactoryAnnotationUtils.qualifiedBeanOfType(
            applicationContext,
            ActionRepository.class,
            database
    );
}

但是,此解决方案不适用于我的自定义注释。它仅在使用标准时有效@Qualifier

知道如何解决这个问题吗?

标签: javaspring

解决方案


这里看来,BeanFactoryAnnotationUtils不支持你的情况。但是我们可以结合 ApplicationContext'sgetBeansOfType()findAnnotationOnBean()来达到同样的目的:

@Bean
@Primary
ActionRepository actionRepository(final ApplicationContext applicationContext,
        final Configuration configuration) {
    final var database = configuration.getString("...");

    Map<String, ActionRepository> beanMap = context.getBeansOfType(ActionRepository.class);

    for (Map.Entry<String, ActionRepository> entry : beanMap.entrySet()) {
        Database db = context.findAnnotationOnBean(entry.getKey(), Database.class);
        if (db != null && db.value().equals(database)) {
            return entry.getValue();
        }
    }
    throw new RuntimeException("Cannot find the bean...");
}

推荐阅读