Spring系列之-Aware系列接口
Aware是一个空接口,其目的是标明一些列接口,这些接口主要用于获取一些上下文的实现。从Spring文档中可以看到,目前一Aware结尾的接口主要有下列接口:[ApplicationContextAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/ApplicationContextAware.html), [ApplicationEventPublisherAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/ApplicationEventPublisherAware.html), [BeanClassLoaderAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/BeanClassLoaderAware.html), [BeanFactoryAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/BeanFactoryAware.html), [BeanNameAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/BeanNameAware.html), [BootstrapContextAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jca/context/BootstrapContextAware.html), [EmbeddedValueResolverAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/EmbeddedValueResolverAware.html), [EnvironmentAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/EnvironmentAware.html), [ImportAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ImportAware.html), [LoadTimeWeaverAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/weaving/LoadTimeWeaverAware.html), [MessageSourceAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/MessageSourceAware.html), [NotificationPublisherAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jmx/export/notification/NotificationPublisherAware.html), [ResourceLoaderAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/ResourceLoaderAware.html), [SchedulerContextAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/quartz/SchedulerContextAware.html), [ServletConfigAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/ServletConfigAware.html), [ServletContextAware](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/ServletContextAware.html)。
下面将对这些接口具体功能及其实现原理做简要分析:
功能介绍:
实现ApplicationContextAware接口的Bean可以获得Spring上下文信息ApplicationContext,ApplicationContextAware接口内部只有一个方法:
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
该方法会在Spring容器加载完后被调用,用户只要实现这个接口就能拿到Spring上下文信息。
2.ApplicationEventPublisherAware
该接口主要用于发布一些事件时使用,接口方法如下:
void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);
实现该接口能获取到ApplicationEventPublisher,这个接口具备发布事件功能。
该接口可以获取到加载当前类的类加载器,
void setBeanClassLoader(ClassLoader classLoader);
这个节课很简单,主要哪个用于获取BeanFactory接口。
实现该接口的Bean能够获取在Spring IOC中bean的名字。
拿到BootstrapContext信息,BootstrapContext暂时不清楚用来干啥,官方解释为:
这提供了一种机制,通过引导环境资源适配器实例时,它是引导。那是,当(开始(bootstrapcontext))在resourceadapter类的方法调用。引导上下文包含对资源适配器实例可用的有用设施的引用。
实现该接口可以获取Spring加载properties文件的属性值,
void setEmbeddedValueResolver(StringValueResolver resolver);
通常是用于一些编程是获取配置信息,常用于工具类中。
void setEnvironment(Environment environment);
实现该接口可以获取到系统的环境变量信息。
加载Spring Bean时织入第三方模块,如AspectJ
主要用于获取国际化相关接口
用于获取通知发布者
初始化时注入ResourceLoader
配合Quartz使用,获取Quartz调度上下文
15.ServletConfigAware, ServletContextAware
web开发过程中获取ServletConfig和ServletContext信息。
网友评论