1.Aware接口简介
Spring的依赖注入的最大亮点是所有的Bean对Spring容器的存在是没有意识的,我们可以将Spring容器换成其他的容器,Spring容器中的Bean的耦合度因此也是极低的。
但是我们在实际的开发中,我们却经常要用到Spring容器本身的功能资源,所以Spring容器中的Bean此时就要意识到Spring容器的存在才能调用Spring所提供的资源。我们通过Spring提供的一系列接口Spring Aware来实现具体的功能。
/**
* 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是一个具有标识作用的超级接口,实现该接口的bean是具有被spring 容器通知的能力的,而被通知的方式就是通过回调。也就是说:直接或间接实现了这个接口的类,都具有被spring容器通知的能力。
常见Aware的作用
- BeanNameAware 获得到容器中Bean的名称
- BeanFactoryAware 获得当前bean Factory,从而调用容器的服务
- ApplicationContextAware 获得当前的application context从而调用容器的服务
- MessageSourceAware 得到message source从而得到文本信息
- ApplicationEventPublisherAware 应用时间发布器,用于发布事件
- ResourceLoaderAware 获取资源加载器,可以获得外部资源文件
Spring Aware的目的是为了让Bean获得Spring容器的服务。因为ApplicationContext接口集成了MessageSource接口、ApplicationEventPublisher接口和ResourceLoader接口,因此当Bean继承自ApplicationContextAware的时候就可以得到Spring容器的所有服务。
- 2.实例演示——ApplicationContextAware
@Component
public class Red implements ApplicationContextAware, BeanNameAware , EmbeddedValueResolverAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext=applicationContext;
System.out.println("传入的IOC容器:"+applicationContext);
}
@Override
public void setBeanName(String name) {
System.out.println("当前bean的名字:"+name);
}
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
String s = resolver.resolveStringValue("您好 ${os.name} 我是 #{10*12}");
System.out.println("解析的字符串是 :"+s);
}
}
- 调用setApplicationContext的调用栈
是在applyBeanPostProcessorsBeforeInitialization中后置处理器ApplicationContextAwareProcessor调用Plane.setApplicationContext的回调函数实现的
一、BeanNameAware是在invokeAwareMethods中进行回调函数调用
二、ApplicationContextAware和EmbeddedValueResolverAware在后置处理器中调用
这两个都是在后置处理器ApplicationContextAwareProcessor中进行回调函数调用的。