什么是spring aware

时间:2026-02-13 12:17:37

1、第一步

在开发中,不可避免需要使用到spring容器本身的功能和资源,这时bean需要意识到spring容器的存在,才能使用spring提供的资源,这就是所谓的spring aware

什么是spring aware

2、第二步

spring Aware的目的是为了将bean获得spring容器的服务,applicationContext接口集成了MessageSource接口、applicationEvenPublisher接口   ResourceLoader接口

什么是spring aware

3、第三步

Bean集成了ApplicationContextArea可以获得spring容器的所有服务,但是原则上还是用到什么接口,就实现什么接口

什么是spring aware

4、第四步

上一步定义了spring容器的工厂类,在项目实际使用中,如下图:

什么是spring aware

5、第五步

实现BeanNameAware/ResourceLoaderAware接口,可以获得Bean名称和加载的服务

实现ResourceLoaderAware需要重写 setresourceLoader

实现beanNameAware需要重写setBeanName方法

什么是spring aware

© 2026 海能知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com