您可能可以使用@PostConstruct
of执行以下操作ClassA
:
@PostConstruct
public void postConstruct(){
SomeAnnoation someAnnotation = this.getClass().getField("bar").getAnnotation(SomeAnnotation.class);
bar.someString(someAnnotation.value());
}
更新: - 使用 BeanPostProcessor 的一般解决方案:
public class SomeAnnotationFieldInitalizer implements BeanPostProcessor{
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Field[] fields = bean.getClass().getFields();
if (fields!=null){
for (Field field:fields){
SomeAnnotation someAnnotation = field.getAnnotation(SomeAnnotation.class);
if (someAnnotation!=null){
try {
ReflectionUtils.makeAccessible(field);
field.set(bean, someAnnotation.value());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return bean;
}
}