ApplicationContext ==>> ListableBeanFactory ==>> BeanFactory
ListableBeanFactory 接口有个findAnnotationOnBean(String beanName, Class<A> annotationType)方法
/**
* 在指定的bean上找到{@code annotationType}的{@link Annotation},如果在给定的类本身上找不到注释,则遍历它的接口和超类。
*/
@Nullable
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) throws NoSuchBeanDefinitionException;
案例:
Map<String, Object> beans = applicationContext.getBeansWithAnnotation(TestAnnotation.class);
for (Map.Entry<String, Object> entry : beans.entrySet()) {
// entry.getKey(), entry.getValue()
TestAnnotation testAnnotation = applicationContext.findAnnotationOnBean(entry.getKey(), TestAnnotation.class);
String value = testAnnotation.value();
.....
}