前言
今天看到一段代码
@Component
public class BaseApplicationContext implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(@SuppressWarnings("NullableProblems")
ApplicationContext applicationContext) throws BeansException {
BaseApplicationContext.context = applicationContext;
}
public static ApplicationContext getContext(){
return context;
}
}
这个代码是用来干什么的呢?它的作用就是用来获取应用上下文(即ApplicationContext )
ApplicationContext
首先,我们了解下什么是ApplicationContext
我们知道,Spring容器是生成Bean实例的工厂,并管理容器中的Bean。生活中我们一般会把生产产品的地方称为工厂,而在这里bean对象的地方官方取名为BeanFactory,直译Bean工厂(com.springframework.beans.factory.BeanFactory),我们一般称BeanFactory为IoC容器,而称ApplicationContext为应用上下文。
BeanFactory提供了OC容器最基本的形式,给具体的IOC容器的实现提供了规范,BeanFactory负责配置、创建、管理Bean等基本功能
ApplicationContext的中文意思是“应用前后关系”,它继承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在国际化支持、资源访问(如URL和文件)、事件传播等方面进行了良好的支持,被推荐为Java EE应用之首选,可应用在Java APP与Java Web中。
ApplicationContextAware
ApplicationContextAware 接口是什么?
实现ApplicationContextAware接口之后,系统启动时就可以自动给我们的服务注入applicationContext对象(自动调用了接口中的setApplicationContext方法),我们就可以获取到ApplicationContext里的所有信息了。
注意:实现ApplicationContextAware接口的类需要交给spring管理,使用@Component或者在配置文件声明bean
我们获取ApplicationContext,一般是用来获取bean的,AoolicationContext有一个getBean方法,此方法继承自BeanFactory
T getBean(Class requiredType) throws BeansException;
实现方法主要有以下几种
public Object getBean(String name) throws BeansException {
this.assertBeanFactoryActive();
return this.getBeanFactory().getBean(name);
}
这个方法通过name来返回一个bean的实例,注意返回的是一个Object 的对象,要进行强制转换
public <T> T getBean(Class<T> requiredType) throws BeansException {
this.assertBeanFactoryActive();
return this.getBeanFactory().getBean(requiredType);
}
这个方法通过Class类型来返回bean,返回的对象即是这个类型的bean对象,无需转换
原理
研究一下ApplicationContextAware 的原理
我们都知道spring的入口方法就在AbstractApplicationContext的refresh()方法.我们先去看看refresh().prepareBeanFactory()方法。
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
.....
// Prepare the bean factory for use in this context.准备bean工厂,以便在此上下文中使用。
prepareBeanFactory(beanFactory);
//在prepareBeanFactory中,我们可以看到addBeanPostProcessor(new ApplicationContextAwareProcessor(this)),将一个ApplicationContextAwareProcessor添加到了BeanPostProcessor中
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// Tell the internal bean factory to use the context's class loader etc.告诉内部bean工厂使用上下文的类加载器等等。
beanFactory.setBeanClassLoader(getClassLoader());
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
// Configure the bean factory with context callbacks.用上下文回调配置bean工厂。
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);
.....
}
ApplicationContextAwareProcessor的构造方法如下
public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {
this.applicationContext = applicationContext;
this.embeddedValueResolver = new EmbeddedValueResolver(applicationContext.getBeanFactory());
}
ApplicationContextAwareProcessor中有两个方法,一个是初始化回调,一个是实现
/**
* Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean初始化回调
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* <p>The default implementation returns the given {@code bean} as-is.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
* if {@code null}, no subsequent BeanPostProcessors will be invoked
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
*/
@Override
@Nullable
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;
}
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);
}
//针对实现了ApplicationContextAware的接口,spring都将调用其setApplicationContext,将applicationContext注入到当前bean对象。
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
}
}