Spring bean生命周期执行顺序

xiaoxiao2021-02-28  87

beanFactory层面


特殊的beanfactory后置处理器 BeanDefinitionRegistryPostProcessor beanfactory后置处理器(PriorityOrdered => Ordered => 普通)

bean层面


bean构造器 beanPostProcessor的postProcessBeforeInitialization方法(PriorityOrdered => Ordered => 普通) PostContructor InitializingBean initMethod

实现了Aware接口的Bean也是由实现了BeanPostProcessor接口的Bean处理的 比如ApplicationContextAwareProcessor注入上下文

private void invokeAwareInterfaces(Object bean) { if (bean instanceof Aware) { if (bean instanceof EnvironmentAware) { ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment()); } if (bean instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver( new EmbeddedValueResolver(this.applicationContext.getBeanFactory())); } if (bean instanceof ResourceLoaderAware) { ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext); } if (bean instanceof ApplicationEventPublisherAware) { ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext); } if (bean instanceof MessageSourceAware) { ((MessageSourceAware) bean).setMessageSource(this.applicationContext); } if (bean instanceof ApplicationContextAware) { // here here here ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext); } } }
转载请注明原文地址: https://www.6miu.com/read-76047.html

最新回复(0)