spring中ApplicationContextAware浅析使用

本文介绍了Spring中实现ApplicationContextAware接口的用法,通过该接口可以在Bean实例化后获取ApplicationContext,进一步获取其他Bean。文章详细阐述了从Bean初始化到ApplicationContext注入的流程,并提及ContextLoaderListener如何加载配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考: https://blog.youkuaiyun.com/andybbc/article/details/50607347

            https://blog.youkuaiyun.com/u011485376/article/details/82633661

基本感言

一个Javabean通过实现ApplicationContextAware接口简直太棒啦,这样就可以在实现类中获取容易上下文applicationContext了,就可以获取想要的bean了,太牛逼了,太感人了,此刻值得感动,更值得哭泣。(心情不好,扯淡玩呢)

基本用法

ApplicationContextAware中有一个public void setApplicationContext(ApplicationContext applicationContext) throws BeansException方法,通过此方法在容器启动时就会把容器上下文设置到参数applicationContext中,这时子类通过此方法就可以直接获取容器上下文,进而获取想要的bean。

详解如下

当我们想要拿到ApplicationContext对象时,我们可以定义一个Bean并实现ApplicationContextAware接口,代码如下。

@Component
public class SpringContextUtil implements ApplicationContextAware {
	
	private static ApplicationContext springContext;
 
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		System.out.println("执行ApplicationContext对象注入!");
		SpringContextUtil.springContext = applicationContext;
	}
	
	public static ApplicationContext getSpringContext() {
		return SpringContextUtil.springContext;
	}
 
}

Spring帮我们注入ApplicationContext的时机是在创建完Bean实例执行initializeBean方法时,initializeBean方法代码如下:

	protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
		if (System.getSecurityManager() != null) {
			AccessController.doPrivileged(new PrivilegedAction<Object>() {
				@Override
				public Object run() {
					invokeAwareMethods(beanName, bean);
					return null;
				}
			}, getAccessControlContext());
		}
		else {
			invokeAwareMethods(beanName, bean);
		}
 
		Object wrappedBean = bean;
		if (mbd == null || !mbd.isSynthetic()) {
                 //对Bean实例进行前置处理,
			wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
		}
 
		try {
                  //调用Bean的实例的初始化方法
			invokeInitMethods(beanName, wrappedBean, mbd);
		}
		catch (Throwable ex) {
			throw new BeanCreationException(
					(mbd != null ? mbd.getResourceDescription() : null),
					beanName, "Invocation of init method failed", ex);
		}
		if (mbd == null || !mbd.isSynthetic()) {
                 //对Bean实例进行后置处理
			wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
		}
		return wrappedBean;
	}

applyBeanPostProcessorsBeforeInitialization方法代码如下

	public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
			throws BeansException {
 
		Object result = existingBean;
		for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
			result = beanProcessor.postProcessBeforeInitialization(result, beanName);
			if (result == null) {
				return result;
			}
		}
		return result;
	}

postProcessBeforeInitialization方法的代码如下

	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() {
                         //在这里会调用ApplicationContextAware接口中的setApplicationContext方法
					invokeAwareInterfaces(bean);
					return null;
				}
			}, acc);
		}
		else {
			invokeAwareInterfaces(bean);
		}
 
		return bean;
	}

invokeAwareInterfaces方法代码如下。

	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) {//判断bean(此bean指实现ApplicationContextAware接口的bean)是否是ApplicationContextAware类型实例,如果是则将bean向上转型成ApplicationContextAware,此时bean的引用指向的还是实现类,执行实现类的setApplicationContext方法,将applicationContext上下文设置进去,我们进而就会获取成功上下文
				((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
			}
		}
	}

以上就是我们通过ApplicationContextAware接口拿到ApplicationContext的过程。

 

题外话:<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>会加载默认路径的application.xml配置文件,想指定加载的文件可以这样如下:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:conf/app-context.xml</param-value>
 </context-param>

注意<param-name>contextConfigLocation</param-name>是不能改变的,此名字不可更改,spring会查找contextConfigLocation的键进而获取指定的配置文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值