3.6.3 Other Aware interfaces
除了上面讨论的ApplicationContextAware
和BeanNameAware
,Spring提供了一系列Aware
接口,允许bean向容器指示他们需要一个基础结构(SpringAPI)依赖。 最重要的Aware
接口总结如下 - 作为一般规则,看名字就能知道依赖类型:
Table 3.4. Aware interfaces
名称 | 注入依赖 | 详情… |
---|---|---|
ApplicationContextAware |
声明 ApplicationContext |
Section 3.6.2, “ApplicationContextAware and BeanNameAware” |
ApplicationEventPublisherAware |
Event publisher of the enclosing ApplicationContext |
Section 3.15, “Additional Capabilities of the ApplicationContext” |
BeanClassLoaderAware |
加载bean的类加载器 | Section 3.3.2, “Instantiating beans” |
BeanFactoryAware |
Declaring BeanFactory |
Section 3.6.2, “ApplicationContextAware and BeanNameAware” |
BeanNameAware |
Name of the declaring bean | Section 3.6.2, “ApplicationContextAware and BeanNameAware” |
BootstrapContextAware |
Resource adapter BootstrapContext the container runs in. Typically available only in JCA aware ApplicationContext s |
Chapter 28, JCA CCI |
LoadTimeWeaverAware |
Defined weaver for processing class definition at load time | Section 7.8.4, “Load-time weaving with AspectJ in the Spring Framework” |
MessageSourceAware |
配置的解决消息的策略(支持参数化和国际化) | Section 3.15, “Additional Capabilities of the ApplicationContext” |
NotificationPublisherAware |
Spring JMX notification publisher | Section 27.7, “Notifications” |
ResourceLoaderAware |
Configured loader for low-level access to resources | Chapter 4, Resources |
ServletConfigAware |
Current ServletConfig the container runs in. Valid only in a web-aware Spring ApplicationContext |
Chapter 18, Web MVC framework |
ServletContextAware |
Current ServletContext the container runs in. Valid only in a web-aware Spring ApplicationContext |
Chapter 18, Web MVC framework |
再次注意,这些接口的使用将您的代码绑定到Spring API,而不遵循控制反转风格。 因此,建议除非有需求的基础bean才使用编程式访问容器。