作用:实现了Aware接口的bean在初始化后可以获取相应资源并进行相应的操作 举例: ApplicationContextAware:向实现了该接口的bean提供IOC容器的上下文信息(ApplicationContext),实现了该接口的bean必须配置到配置文件中并由spring的bean容器加载 BeanNameAware:向实现了该接口的bean提供关于beanName定义的相关内容
其他就做不介绍了,官网文档内容如下:
Table 7.4. Aware interfaces
Name | Injected Dependency |
Explained in… |
---|---|---|
ApplicationContextAware
|
Declaring ApplicationContext
|
Section 7.6.2, “ApplicationContextAware and BeanNameAware” |
ApplicationEventPublisherAware
|
Event publisher of the enclosing ApplicationContext
|
Section 7.15, “Additional Capabilities of the ApplicationContext” |
BeanClassLoaderAware
|
Class loader used to load the bean classes. |
Section 7.3.2, “Instantiating beans” |
BeanFactoryAware
|
Declaring BeanFactory
|
Section 7.6.2, “ApplicationContextAware and BeanNameAware” |
BeanNameAware
|
Name of the declaring bean |
Section 7.6.2, “ApplicationContextAware and BeanNameAware” |
BootstrapContextAware
|
Resource adapter BootstrapContext thecontainer runs in. Typically available only in JCA aware ApplicationContext s
|
Chapter 32, JCA CCI |
LoadTimeWeaverAware
|
Defined weaver for processing class definition at load time |
Section 11.8.4, “Load-time weaving with AspectJ in the Spring Framework” |
MessageSourceAware
|
Configured strategy for resolving messages (with support for parametrization and internationalization) |
Section 7.15, “Additional Capabilities of the ApplicationContext” |
NotificationPublisherAware
|
Spring JMX notification publisher |
Section 31.7, “Notifications” |
PortletConfigAware
|
Current PortletConfig thecontainer runs in. Valid only in a web-aware Spring ApplicationContext
|
Chapter 25, Portlet MVC Framework |
PortletContextAware
|
Current PortletContext thecontainer runs in. Valid only in a web-aware Spring ApplicationContext
|
Chapter 25, Portlet MVC Framework |
ResourceLoaderAware
|
Configured loader for low-level access to resources |
Chapter 8, Resources |
ServletConfigAware
|
Current ServletConfig thecontainer runs in. Valid only in a web-aware Spring ApplicationContext
|
Chapter 22, Web MVC framework |
ServletContextAware
|
Current ServletContext thecontainer runs in. Valid only in a web-aware Spring ApplicationContext
|
Chapter 22, Web MVC framework |
Resource
Resources (针对于资源文件的统一接口) A、UrlResource:URL 对应的资源,根据一个 URL 地址即可获取 B、ClassPathResource:获取类路径下的资源 C、FileSystemResource:获取文件系统里面的资源 D、ServletContextResource:ServletContext 封装的资源,用于访问 ServletContext 环境下的资源 E、InputStreamResource:获取输入流封装的资源 F、ByteArrayResource:获取字节数组封装的资源 通过实现 ApplicationContextAware 接口中的方法 ,因为需要实现ResourceLoader,而
ApplicationContextAware 实现了ResourceLoader
setApplicationContext(ApplicationContext applicationContext)Resource resource = applicationContext.getResource("xxx");具体演示示例见github
项目地址:https://github.com/DaleyChao/SpringPractice/commit/e84bffe0d2c72fa853d9bb1ffc4d4186e5de0e10
时间: 2024-09-19 15:01:27