BeanFactoryPostProcessor扩展

@Configuration
@ComponentScan("com.example")
public class AppConfig {
	
}
@Component
public class User {
	public User() {
	}

	private String name;
	private Integer age;

	public User(String name, Integer age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User{" +
				"name='" + name + '\'' +
				", age=" + age +
				'}';
	}
}
@Component
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessBeanFactory");
		//获取BeanDefinition
		BeanDefinition beanDefinition = beanFactory.getBeanDefinition("user");
		System.out.println("修改属性name值");
		beanDefinition.getPropertyValues().add("name", "tom");

	}


}

测试方法

@Test
	public void testBeanFactoryProcessor(){
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( AppConfig.class );
		User user = (User) context.getBean( User.class );
		System.out.println(user);

	}

测试结果

postProcessBeanFactory
修改属性name值
User{name='tom', age=null}

你可能感兴趣的:(Spring源码,java)