参考: 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的键进而获取指定的配置文件。