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...");
}
添加回答
舉報