解析BeanPostProcessor系列之ApplicationContextAwareProcessor

ApplicationContextAwareProcessor直接实现了BeanPostProcessor接口,其作用主要是为一些Aware类型的接口,注入对应的属性值

主要就是处理以下六种。
EnvironmentAware, EmbeddedValueResolverAware, ResourceLoaderAware ,ApplicationEventPublisherAware,MessageSourceAware,ApplicationContextAware

在这里插入图片描述

源码部分

class ApplicationContextAwareProcessor implements BeanPostProcessor {
    
    

	private final ConfigurableApplicationContext applicationContext;

	private final StringValueResolver embeddedValueResolver;


	/**
	 * Create a new ApplicationContextAwareProcessor for the given context.
	 */
	public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {
    
    
		this.applicationContext = applicationContext;
		this.embeddedValueResolver = new EmbeddedValueResolver(applicationContext.getBeanFactory());
	}

	
	@Override
	@Nullable
	//bean实例化之后,初始化之前会调用到这个方法
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    
    
		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;
	}

	//调用Aware接口的setXXX方法,实现对应属性的赋值
	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);
		}
	}

}

一般我们常用到的ApplicationContextAware,通过实现这个接口拿到applicationContext,进而获取容器中的bean,或者其他信息。

具体流程也很简单,refresh方法入口,在调用initializeBean方法时被调用到。
在这里插入图片描述

提供从spring容器中获取bean对象的工具类。

@Configuration
public class SpringBeanUtil implements ApplicationContextAware {
    
    

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    
    
        this.applicationContext = applicationContext;
    }

    public Object getBean(String beanName) {
    
    
        return applicationContext.getBean(beanName);
    }

}
    @Test
    public void test() {
    
    
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.learn.spring");
        SpringBeanUtil springBeanUtil = (SpringBeanUtil) applicationContext.getBean("springBeanUtil");
        //从AnnotationConfigApplicationContext中获取bean
        System.out.println(springBeanUtil);
        //从我们自己的工具类中获取bean
        System.out.println(springBeanUtil.getBean("springBeanUtil"));
    }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/CSDN_WYL2016/article/details/107792193