我创造了一个定制的春天
@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中只有一个必须可用于注入,所以我创建了一个
@Primary
豆法。
@Bean
@Primary
ActionRepository actionRepository(
final ApplicationContext applicationContext,
final Configuration configuration) {
final var database = configuration.getString("...");
return BeanFactoryAnnotationUtils.qualifiedBeanOfType(
applicationContext,
ActionRepository.class,
database
);
}
但是这个解决方案
不
使用我的自定义批注。只有在使用标准时才有效
限定词
一个。
我知道如何解决这个问题吗?