SpringBoot(三)

编程

beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)):加入对ApplicationContextAware的支持

在Spring注解开发(二、生命周期) 中提到过,ApplicationContextAware的实现原理也是BeanPostProcessor

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {

//pplicationContextAware的实现

beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

beanFactory.ignoreDependencyInterface(EnvironmentAware.class);

beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);

beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);

beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);

beanFactory.ignoreDependencyInterface(MessageSourceAware.class);

beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

}

class ApplicationContextAwareProcessor implements BeanPostProcessor {

@Override

public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {

AccessControlContext acc = null;

if (System.getSecurityManager() != null &&

(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||

bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||

bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {

acc = this.applicationContext.getBeanFactory().getAccessControlContext();

}

if (acc != null) {

AccessController.doPrivileged(new PrivilegedAction<Object>() {

@Override

public Object run() {

invokeAwareInterfaces(bean);

return null;

}

}, acc);

}

else {

invokeAwareInterfaces(bean);

}

return bean;

}

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(this.embeddedValueResolver);

}

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) {

((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);

}

}

}

}

同时由此可见,ApplicationContextAware要早于BeanPostProcessor发生

beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this))加入对ApplicationListener的支持

class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor {

@Override

public Object postProcessAfterInitialization(Object bean, String beanName) {

if (bean instanceof ApplicationListener) {

// potentially not detected as a listener by getBeanNamesForType retrieval

Boolean flag = this.singletonNames.get(beanName);

if (Boolean.TRUE.equals(flag)) {

// singleton bean (top-level or inner): register on the fly

this.applicationContext.addApplicationListener((ApplicationListener<?>) bean);

}

else if (Boolean.FALSE.equals(flag)) {

if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) {

// inner bean with other scope - can"t reliably process events

logger.warn("Inner bean "" + beanName + "" implements ApplicationListener interface " +

"but is not reachable for event multicasting by its containing ApplicationContext " +

"because it does not have singleton scope. Only top-level listener beans are allowed " +

"to be of non-singleton scope.");

}

this.singletonNames.remove(beanName);

}

}

return bean;

}

以上是 SpringBoot(三) 的全部内容, 来源链接: utcz.com/z/517635.html

回到顶部