beanFactoryPostProcesstor和BeanPostProcessor是spring的钩子函数,是spring的bean初始化对外暴露的扩展点。
spring IOC容器允许BeanFactoryPostProcessor在容器初始化任何bean之前读取bean的定义并且修改其定义,比如将懒加载改为true等。只要实现了BeanFactoryPostProcessor接口,并且重写了postProcessorBeanFactory方法,spring在beanDefinition注册完,并执行这个后置处理器。这里我将名字为“loginService”的beanDefinition修改其懒加载为true。(spring懒加载默认为false)
注意:在自定义beanFactoryPostProcesstor中不能通过beanFactory去获取容器中bean。比如beanFactory.getBean("xxx");会导致xxx这个bean类引用(比如@Autowire User user)属性为空。因为beanFactoryPostProcesstor执行的阶段spring还未将每个bean整个实例化完。所以在调用getBean的时候会直接去生成这个对象,并且给对象属性包装。当spring发现xxx里面引用了user标注@Autowire就会从容器中找"user"这个bean但是此时"user"这个bean还未实例化,所以为空。
spring--beanFactoryPostProcesstor
最新推荐文章于 2023-02-24 10:19:03 发布