之前一直不太知道Aware这个东西有什么用,看了看官网,别人的博客,今天算是有点眉目了,在此做一个笔记
在Spring中有一个接口,但是这个接口中没有一个方法,那么它有什么用呢,通过注释知道,这个接口起到的是一个标记作用,或者说的高大一些,做到面向接口编程,具体需要实现的工作还是需要子类来实现
/**
* A marker superinterface indicating that a bean is eligible to be notified by the
* Spring container of a particular framework object through a callback-style method.
* The actual method signature is determined by individual subinterfaces but should
* typically consist of just one void-returning method that accepts a single argument.
*
* <p>Note that merely implementing {@link Aware} provides no default functionality.
* Rather, processing must be done explicitly, for example in a
* {@link org.springframework.beans.factory.config.BeanPostProcessor}.
* Refer to {@link org.springframework.context.support.ApplicationContextAwareProcessor}
* for an example of processing specific {@code *Aware} interface callbacks.
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
*/
public interface Aware {
}
子类继承这个接口能做什么,我们先看看它有哪些子接口
如上图,我们看到了Aware有这么多子接口,而aware单词本身有知道、感知的作用,结合官网,别人的博客知道,在spring实例化bean的过程中,如果我们定义的bean实现了这些Aware,那么spring就会感知到这些Bean需要的东西
举例:BeanNameAware
如果我们定义的类实现了这个接口,那么在实例化这个bean的时候,通过后置处理器执行到BeanNameAware的setBeanName方法(),其他的Aware也是同样的原理,只是针对不同的Aware,具体调用的时机可能有所不同,具体的我们后续分析
需要注意的是,我们自己定义一个Aware在Spring中是没有办法识别的,原因我们看看源码就知道了
举例说明,自定义一个Bean,实现ApplicationContextAware
/**
* @ClassName ApplicationContextAwareBeanTest
* @Author mjlft
* @Date 2020/2/9 14:00
* @Version 1.0
* @Description 测试ApplicationContextAware的调用时机
*/
@Component
public class ApplicationContextAwareBeanTest implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("在这里执行ApplicationContextAware");
}
}
根据方法调用栈可以知道, 在applicationrefresh过程中创建bean时,会执行如下方法
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareMethods(beanName, bean);
return null;
}, getAccessControlContext());
} else {
//这个地方调用接口Aware的方法,
//例如当前bean实现了ApplicationContextAware接口,那么这个地方会调用这个接口的
invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
//执行bean后置处理,包括自定义的还有系统定义的0,
// @PostCounstruct由系统提供的CommonAnnotationBeanPostProcessor进行处理
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
//这里执行实现InitializingBean接口的方法
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()) {
//执行beanPostProcessor的applyBeanPostProcessorsAfterInitialization方法
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
这个方法最开始就调用了invokeAwareMethods(beanName, bean);而这里的这个方法只处理了几个Aware,我们测试样例中的ApplicationContextAware也不是在这里执行的,所以还得跟踪方法栈
private void invokeAwareMethods(final String beanName, final Object bean) {
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
ClassLoader bcl = getBeanClassLoader();
if (bcl != null) {
((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
}
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
最后在ApplicationContextAwareProcessor的一个方法中知道了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) {
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
}
}
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
}
}
}
当然我们这里只是一个例子,可能对于别的Aware,在其他的后置处理中进行了处理,总之原理我们是知道,为什么实现了Aware接口后,spring能够调用到方法从而实现我们所谓的感知