Aware 接口
从 Spring 官方文档 中我们知道,Spring
底层为我们提供了一系列的 Aware
回调接口,来提供我们开发人员访问 Spring
底层组件的能力。比如常用的:ApplicationContextAware
、BeanNameAware
、BeanFactoryAware
public interface Aware {
}
ApplicationContextAware 接口
定义一个 Bean
,实现 ApplicationContextAware
和 BeanNameAware
接口:
public class Red implements ApplicationContextAware, BeanNameAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("当前ioc容器为:" + applicationContext);
this.applicationContext = applicationContext;
}
@Override
public void setBeanName(String name) {
System.out.println("当前 Bean 的名字:" + name);
}
}
创建 IoC 容器
,并使用 @Import
导入组件到 IoC 容器
中,输出结果:
当前 Bean 的名字:com.keke.bean.Red
当前ioc容器为:org.springframework.context.annotation.AnnotationConfigApplicationContext@305b7c14, started on Thu Jul 28 18:17:52 CST 2022
原理
在 Spring - IoC 容器之拓展点 BeanPostProcessor 这篇文章中我们知道,Spring
使用 BeanPostProcessor
在 Bean
的初始化前后执行自定义的逻辑。这里我们使用 Spring
提供的 xxxAware
能力,其实本质上也是利用 BeanPostProcessor
在 Bean
的初始化阶段执行预定义的逻辑而实现的。比如 ApplicationContextAware
对应的 ApplicationContextAwareProcessor
:
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
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// 如果 Bean 实现了以下几种 Aware,则会继续执行下面的逻辑
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);
}
}
}