-
用途
BeanFactoryPostProcessor表示Bean工厂的后置处理器,其实和BeanPostProcessor类似,BeanPostProcessor是干涉Bean的创建过程,BeanFactoryPostProcessor是干涉BeanFactory的创建过程。我们可以在postProcessBeanFactory()方法中对BeanFactory进行加工。 -
自定义MyBeanFactoryPostProcessor
@Component public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { UserServiceImpl userService = (UserServiceImpl) beanFactory.getBean("userService"); userService.setColor("yellow"); } }
@Service("userService") public class UserServiceImpl implements IUserService{ private String color; public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
结论:在UserServiceImpl中,没对color属性进行赋值,但是在自定义的MyBeanFactoryPostProcessor(自定义的bean工厂后置处理器)中对其进行了设置值,所以在最后获取color的时候,就能获取到值
spring使用BeanFactoryPostProcessor(Bean工厂的后置处理器)
最新推荐文章于 2023-04-17 07:49:41 发布