Spring bean 实现了如下 Aware 接口,可以注入相关对象:

BeanFactoryAware,                //获取 IoC 容器 BeanFactory 的能力
ApplicationContextAware,         //获取 Spring 应用上下文 ApplicationContext 对象的能力
EnvironmentAware,                //获取 Environment 对象的能力
ResourceLoaderAware,             //获取资源加载器对象 ResourceLoader 的能力
BeanClassLoaderAware,            //获取加载当前 Bean Class 的 ClassLoader 的能力
BeanNameAware,                   //获取当前 Bean 名称的能力
MessageSourceAware,              //获取用于国际化 MessageSource 对象的能力
ApplicationEventPublisherAware,  //获取事件发布对象 ApplicationEventPublisher 的能力
EmbeddedValueResolverAware       //获取占位符处理对象 StringValueResolver 的能力

示例代码:

spring xml 配置

<bean name="user1" class="constxiong.User"><property name="id" value="1"/>
</bean>

User 类

public class User implementsBeanFactoryAware,                //获取 IoC 容器 BeanFactory 的能力ApplicationContextAware,         //获取 Spring 应用上下文 ApplicationContext 对象的能力EnvironmentAware,                //获取 Environment 对象的能力ResourceLoaderAware,             //获取资源加载器对象 ResourceLoader 的能力BeanClassLoaderAware,            //获取加载当前 Bean Class 的 ClassLoader 的能力BeanNameAware,                   //获取当前 Bean 名称的能力MessageSourceAware,              //获取用于国际化 MessageSource 对象的能力ApplicationEventPublisherAware,  //获取事件发布对象 ApplicationEventPublisher 的能力EmbeddedValueResolverAware       //获取占位符处理对象 StringValueResolver 的能力
{private BeanFactory beanFactory;private ApplicationContext applicationContext;private Environment environment;private ResourceLoader resourceLoader;private ClassLoader beanClassLoader;private String beanName;private MessageSource messageSource;private ApplicationEventPublisher applicationEventPublisher;private StringValueResolver embeddedValueResolver;private Integer id;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}@Overridepublic String toString() {return "User{" +"beanFactory=" + beanFactory + '
' +"applicationContext=" + applicationContext + '
' +"environment=" + environment + '
' +"resourceLoader=" + resourceLoader + '
' +"beanClassLoader=" + beanClassLoader + '
' +"beanName='" + beanName + ''' + '
' +"messageSource=" + messageSource + '
' +"applicationEventPublisher=" + applicationEventPublisher + '
' +"embeddedValueResolver=" + embeddedValueResolver + '
' +"id=" + id +'}';}@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {this.beanFactory = beanFactory;}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}@Overridepublic void setEnvironment(Environment environment) {this.environment = environment;}@Overridepublic void setResourceLoader(ResourceLoader resourceLoader) {this.resourceLoader = resourceLoader;}@Overridepublic void setBeanClassLoader(ClassLoader beanClassLoader) {this.beanClassLoader = beanClassLoader;}@Overridepublic void setBeanName(String name) {this.beanName = name;}@Overridepublic void setMessageSource(MessageSource messageSource) {this.messageSource = messageSource;}@Overridepublic void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {this.applicationEventPublisher = applicationEventPublisher;}@Overridepublic void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {this.embeddedValueResolver = stringValueResolver;}
}

测试代码

/*** 测试 Bean 通过接口方式回调注入系统内置 bean* @author ConstXiong*/
@Configuration
public class Test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring-interface-callback-inject.xml");User user = context.getBean("user1", User.class);System.out.println(user);}
}

打印结果

User{beanFactory=org.springframework.beans.factory.support.DefaultListableBeanFactory@1a407d53: defining beans [user1]; root of factory hierarchy
applicationContext=org.springframework.context.support.ClassPathXmlApplicationContext@6193b845, started on Thu Feb 04 14:21:00 CST 2021
environment=StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[PropertiesPropertySource {name='systemProperties'}, SystemEnvironmentPropertySource {name='systemEnvironment'}]}
resourceLoader=org.springframework.context.support.ClassPathXmlApplicationContext@6193b845, started on Thu Feb 04 14:21:00 CST 2021
beanClassLoader=sun.misc.Launcher$AppClassLoader@18b4aac2
beanName='user1'
messageSource=org.springframework.context.support.ClassPathXmlApplicationContext@6193b845, started on Thu Feb 04 14:21:00 CST 2021
applicationEventPublisher=org.springframework.context.support.ClassPathXmlApplicationContext@6193b845, started on Thu Feb 04 14:21:00 CST 2021
embeddedValueResolver=org.springframework.beans.factory.config.EmbeddedValueResolver@520a3426
id=1}

完整代码:017-spring-interface-callback-inject


【Java学习资源】整理推荐

  • 自定义 Bean 作用域
  • Bean 的作用域
  • maven 集成 tomcat 以 jar 的方式打包 web 应用
  • Spring 依赖注入与依赖查找来源的区别
  • Spring 依赖注入的处理过程与 DependencyDescriptor 的说明
  • Spring 各种 Aware 接口回调注入
  • Spring Bean 生命周期内的 Exception 复现
  • Spring 内建 Bean
  • Spring Bean 的别名
  • Spring Bean 未指定名称的命名规则
  • Bean 何时被 GC
  • Spring Bean 延迟加载
  • ObjectFactory 与 BeanFactory 的区别
  • Bean 销毁的方式与顺序
  • Bean 初始化的方式与顺序
  • Bean 的实例化方式
  • Bean的注册方式
  • 什么是 BeanDefinition?
  • Spring IoC 容器启动时做了什么?
  • BeanFactory 与 FactoryBean 的区别
  • BeanFactory 与 ApplicationContext 的区别
  • Spring IoC 依赖注入的实现方式
  • Spring IoC 依赖注入(支持哪些数据类型?)
  • Spring bean 依赖查找
  • Spring-IoC
  • Spring 的核心特性

【Java面试题与答案】整理推荐

  • 基础与语法
  • 集合
  • 网络编程
  • 并发编程
  • Web
  • 安全
  • 设计模式
  • 框架
  • 算法与数据结构
  • 异常
  • 文件解析与生成
  • Linux
  • MySQL
  • Oracle
  • Redis
  • Dubbo

Spring 各种 Aware 接口回调注入相关推荐

  1. spring之Aware接口

    Aware接口介绍   Aware是一个具有标识作用的超级接口,具体实现是有子接口去决定的,但是子接口至少要有一个带一个参数的且返回是空的方法.实现该接口的bean是具有被spring 容器通知的能力 ...

  2. spring整合mybatis接口无法注入问题

    在学习Spring完之后简单的了解了MyBatis.然后进行简单的整合,遇到MyBatista接口映射的Bean无法自动注入的问题: 代码异常: 线程"main"org.sprin ...

  3. Spring Aware接口注入

    Aware自动注入 BeanNameAware BeanFactoryAware ApplicationContextAware MessageSourceAware ApplicationEvent ...

  4. 框架:Spring之Aware相关接口

    一.Aware相关接口 对于应用程序来说,应该尽量减少对Sping Api的耦合程度,然而有些时候为了运用Spring所提供的一些功能,有必要让Bean了解Spring容器对其进行管理的细节信息,如让 ...

  5. Spring Aware接口详解

    若 Spring 检测到 bean 实现了 Aware 接口,则会为其注入相应的依赖.所以通过让bean 实现 Aware 接口,则能在 bean 中获得相应的 Spring 容器资源. Spring ...

  6. Spring Bean的生命周期及接口回调

    本篇介绍Spring框架为Spring Bean生命周期各阶段提供的回调接口,程序通过实现回调接口,可以在IOC容器实例化或销毁Bean的过程中,得到Bean的控制权,并对Bean进行预处理工作.通过 ...

  7. spring中的Aware接口原来是这么回事

    一.介绍   使用spring开发的同学,或多或少都使用过形如XxxAware这样的接口.spring文档中是这样解释Aware接口的: Spring提供了广泛的Aware回调接口,让bean向容器表 ...

  8. spring中的Aware接口的作用以及代码剖析

    前言 不知道大家有没有遇到这样的场景,比如,我自己有一个PersonService,  实现类是PersonServiceImpl,我想在PersonServiceImpl中使用application ...

  9. spring中最重要的一些Aware接口

    附上关于这节的spring官方文档: ApplicationContextAware and BeanNameAware aware接口在spring中无处不在,它是用来感知spring的ioc co ...

最新文章

  1. iOS之UI--转场动画
  2. cJSON库源码分析
  3. swoole UDP TCP客户端
  4. 标准库中的智能指针shared_ptr
  5. 基因组与数据整合:DNA应用开发正在临近
  6. JDK 12附带紧凑数字格式
  7. c++构造函数详解(转)
  8. 微软发布“史无前例”的恶意软件数据集,设17万奖金征集预测算法
  9. 大数据分析平台架构(Big Data Analytics Platform)
  10. 网吧计费管理系统(武汉理工大学大一下实验(C语言版源码))
  11. Thinkphp6 think-queue redis 执行异步任务
  12. 软件测试女生可以学习么?现在还能入行么?
  13. 史玉柱自述创业历程,我思故我在
  14. C++ 使用fdk-aac对音频编码
  15. Mind Manager 13 值得购买吗?一次糟糕的购物体验
  16. 现代数学观,何处寻?
  17. java 练习03 - 判断是否是闰年
  18. 如何保护个人的隐私安全
  19. 2020速卖通入驻条件以及费用现在来做速卖通晚不晚?速卖通还好做吗?
  20. 弘辽科技:小伙退伍网上创业卖特产,教你免费如何开淘宝网店

热门文章

  1. 什么是S参数? S参数的含义?
  2. 《赢在中国蓝天碧水间》观后感
  3. Java-7 打印等腰直角三角形
  4. 快排为什么不稳定 举例说明
  5. 微信小程序canvas商品分享海报
  6. 把计算边界层高度的公式放入模式
  7. 小时 分钟 秒 计算
  8. Excel中金额小写转大写,职场小技能,值得学习
  9. Confluence7 配置docker Nginx https 反向代理
  10. AlphaFold/run_alphafold.py代码阅读理解