1 回答
TA贡献1757条经验 获得超7个赞
从这里看来,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...");
}
添加回答
举报