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