ApplicationContextAwareProcessor介绍

ApplicationContextAwareProcessor Bean后置处理器介绍

当前Bean是如下类型:EnvironmentAware 、EmbeddedValueResolverAware、ResourceLoaderAware、ApplicationEventPublisherAware、MessageSourceAware、ApplicationContextAware 中的一个 就会调用对应的回调方法

源码

class ApplicationContextAwareProcessor implements BeanPostProcessor {
    /// 代码省略 。。。。。。。。。。。。。
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        // 当前Bean是如下类型:EnvironmentAware 、EmbeddedValueResolverAware、ResourceLoaderAware、ApplicationEventPublisherAware、MessageSourceAware、ApplicationContextAware 中的一个 就会调用对应的回调方法
		if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
				bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
				bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)){
			return bean;
		}

		AccessControlContext acc = null;

		if (System.getSecurityManager() != null) {
			acc = this.applicationContext.getBeanFactory().getAccessControlContext();
		}

		if (acc != null) {
			AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
				invokeAwareInterfaces(bean);
				return null;
			}, acc);
		}
		else {
			invokeAwareInterfaces(bean);
		}

		return bean;
	}
	
    private void invokeAwareInterfaces(Object bean) {
        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);
        }
    }
    /// 代码省略 。。。。。。。。。。。。。
} 

使用案例:

获取上下文信息


@ConditionalOnMissingBean({ApplicationPropertyPlaceHolder.class})
@ConditionalOnClass(RefreshScope.class)
public class MyConfiguration  implements ApplicationContextAware{

	 private ApplicationContext applicationContext;
 
	 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
	        this.applicationContext = applicationContext;
	 }
	@Bean
    public RefreshScope refreshScope() {
        RefreshScope refreshScope = new RefreshScope();
        refreshScope.setApplicationContext(this.applicationContext);
        return refreshScope;
    }
}


public class ApplicationPropertyPlaceHolder extends PlaceholderConfigurerSupport implements ConfigChangeListener,
        EnvironmentAware, DisposableBean, PropertySourceLocator {


   // something
   
}

你可能感兴趣的:(spring,前端,java,javascript)