我用AnnotationConfigApplicationContext
. 当我调用BeanDefinition.getPropertyValues()
一个BeanFactoryPostProcessor
实现时,我得到了一个空列表。但是如果我用 配置了 spring ClasspathXmlApplicationContext
,它会返回正确的值。有谁知道原因?
这是我的代码:
@Configuration
public class AppConfig {
@Bean
public BeanFactoryPostProcessorTest beanFactoryPostProcessorTest(){
return new BeanFactoryPostProcessorTest();
}
@Bean
public Person person(){
Person person = new Person();
person.setName("name");
person.setAge(18);
return person;
}
}
public class BeanFactoryPostProcessorTest implements BeanFactoryPostProcessor{
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinition bd = beanFactory.getBeanDefinition("person");
MutablePropertyValues propertyValues = bd.getPropertyValues();
if(propertyValues.contains("name")){
propertyValues.addPropertyValue("name","zhangfei");
}
System.out.println("Factory modfy zhangfei");
}
}
public class MainClass {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
/*ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("context.xml");*/
Person person = context.getBean(Person.class);
System.out.println(person.getName());
}
}
非常感谢。