最近,不少小伙伴出去面试都被问到了Spring问题,关于Spring,细节点不少,面试官也很是喜欢问一些很细节的技术点。因此,在 Spring 专题中,咱们尽可能把Spring的每一个技术细节说清楚,将透彻。java
关注 冰河技术 微信公众号,回复 “ Spring注解 ” 关键字领取源码。面试
若是文章对你有所帮助,欢迎你们留言、点赞、在看和转发,你们的支持是我持续创做的动力!spring
自定义组件要想使用Spring容器底层的一些组件(好比:ApplicationContext、BeanFactory等),此时,只须要让自定义组件实现XxxAware接口便可。此时,Spring在建立对象的时候,会调用XxxAware接口定义的方法,注入相关的组件。设计模式
其实,咱们以前使用过XxxAware接口,例如,咱们以前建立的Employee类,就实现了ApplicationContextAware接口,Employee类的源码以下所示。bash
package io.mykit.spring.plugins.register.bean; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * @author binghe * @version 1.0.0 * @description 测试ApplicationContextAware */ @Component public class Employee implements ApplicationContextAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
从Employee类的源码能够看出,实现ApplicationContextAware接口的话,须要实现setApplicationContext()方法。在IOC容器启动并建立Employee对象时,Spring会调用setApplicationContext()方法,而且会将ApplicationContext对象传入到setApplicationContext()方法中,咱们只须要在Employee类中定义一个ApplicationContext类型的成员变量来接收setApplicationContext()方法的参数,就可使用ApplicationContext对象了。微信
其实,在Spring中,相似于ApplicationContextAware接口的设计有不少,本质上,Spring中相似XxxAware接口都继承了Aware接口,咱们来看下Aware接口的源码,以下所示。并发
package org.springframework.beans.factory; /** * @author Chris Beams * @author Juergen Hoeller * @since 3.1 */ public interface Aware { }
能够看到,Aware接口是Spring 3.1版本中引入的接口,在Aware接口中,并未定义任何方法。app
接下来,咱们看看都有哪些接口继承了Aware接口,以下所示。分布式
接下来,咱们就挑选几个经常使用的XxxAware接口来进行简单的说明。ide
ApplicationContextAware接口使用的比较多,咱们先来讲说这个接口,经过ApplicationContextAware接口咱们能够获取到IOC容器。
首先,咱们建立一个Blue类,并实现ApplicationContextAware接口,在实现的setApplicationContext()中将ApplicationContext输出,以下所示。
package io.mykit.spring.plugins.register.bean; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * @author binghe * @version 1.0.0 * @description 测试ApplicationContextAware接口 */ public class Blue implements ApplicationContextAware { private ApplicationContext applicationContext @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("传入的ioc:" + applicationContext); this.applicationContext = applicationContext; } }
咱们也能够为Blue类同时实现几个XxxAware接口,例如,使Blue类再实现一个BeanNameAware接口,咱们能够经过BeanNameAware接口获取到当前bean在Spring容器中的名称,以下所示。
package io.mykit.spring.plugins.register.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanNameAware; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * @author binghe * @version 1.0.0 * @description 测试ApplicationContextAware接口 */ public class Blue implements ApplicationContextAware, BeanNameAware { private ApplicationContext applicationContext @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("传入的ioc:" + applicationContext); this.applicationContext = applicationContext; } @Override public void setBeanName(String name) { System.out.println("当前bean的名字"); } }
接下来,咱们再实现一个EmbeddedValueResolverAware接口,咱们经过EmbeddedValueResolverAware接口可以获取到StringValue解析器。以下所示。
package io.mykit.spring.plugins.register.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanNameAware; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.EmbeddedValueResolverAware; import org.springframework.util.StringValueResolver; /** * @author binghe * @version 1.0.0 * @description 测试ApplicationContextAware接口 */ public class Blue implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("传入的ioc:" + applicationContext); this.applicationContext = applicationContext; } @Override public void setBeanName(String name) { System.out.println("当前bean的名字"); } @Override public void setEmbeddedValueResolver(StringValueResolver resolver) { String resolveStringValue = resolver.resolveStringValue("你好${os.name} 年龄:#{20*18}"); System.out.println("解析后的字符串为:" + resolveStringValue); } }
接下来,咱们须要在Blue类上标注@Component注解将Blue类添加到IOC容器中,以下所示。
@Component public class Blue implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {
运行AutowiredTest类的testAutowired02()方法,输出的结果信息以下所示。
postProcessBeforeInitialization...autowiredConfig=>io.mykit.spring.plugins.register.config.AutowiredConfig$$EnhancerBySpringCGLIB$$d3c83622@1c93084c postProcessAfterInitialization...autowiredConfig=>io.mykit.spring.plugins.register.config.AutowiredConfig$$EnhancerBySpringCGLIB$$d3c83622@1c93084c postProcessBeforeInitialization...personDao=>PersonDao{remark='1'} postProcessAfterInitialization...personDao=>PersonDao{remark='1'} postProcessBeforeInitialization...personDao2=>PersonDao{remark='2'} postProcessAfterInitialization...personDao2=>PersonDao{remark='2'} postProcessBeforeInitialization...personService=>PersonService{personDao=PersonDao{remark='2'}} postProcessAfterInitialization...personService=>PersonService{personDao=PersonDao{remark='2'}} postProcessBeforeInitialization...personController=>io.mykit.spring.plugins.register.controller.PersonController@48ae9b55 postProcessAfterInitialization...personController=>io.mykit.spring.plugins.register.controller.PersonController@48ae9b55 执行了Animal类的无参数构造方法 postProcessBeforeInitialization...animal=>io.mykit.spring.plugins.register.bean.Animal@c267ef4 执行了Animal类的初始化方法。。。。。 postProcessAfterInitialization...animal=>io.mykit.spring.plugins.register.bean.Animal@c267ef4 当前bean的名字:blue 解析后的字符串为:你好Windows 10 年龄:360 传入的ioc:org.springframework.context.annotation.AnnotationConfigApplicationContext@5ecddf8f, started on Wed Aug 19 00:10:13 CST 2020 postProcessBeforeInitialization...blue=>io.mykit.spring.plugins.register.bean.Blue@55182842 postProcessAfterInitialization...blue=>io.mykit.spring.plugins.register.bean.Blue@55182842 Cat类的构造方法... postProcessBeforeInitialization...cat=>io.mykit.spring.plugins.register.bean.Cat@76505305 Cat的postConstruct()方法... postProcessAfterInitialization...cat=>io.mykit.spring.plugins.register.bean.Cat@76505305 调用了Dog的有参构造方法 postProcessBeforeInitialization...dog=>Dog{cat=io.mykit.spring.plugins.register.bean.Cat@76505305} postProcessAfterInitialization...dog=>Dog{cat=io.mykit.spring.plugins.register.bean.Cat@76505305} postProcessBeforeInitialization...employee=>io.mykit.spring.plugins.register.bean.Employee@74235045 postProcessAfterInitialization...employee=>io.mykit.spring.plugins.register.bean.Employee@74235045 postProcessBeforeInitialization...fish=>Fish{cat=io.mykit.spring.plugins.register.bean.Cat@76505305} postProcessAfterInitialization...fish=>Fish{cat=io.mykit.spring.plugins.register.bean.Cat@76505305} Fish{cat=io.mykit.spring.plugins.register.bean.Cat@76505305} Cat的preDestroy()方法... 执行了Animal类的销毁方法。。。。。
输出结果中有以下信息。
当前bean的名字:blue 解析后的字符串为:你好Windows 10 年龄:360 传入的ioc:org.springframework.context.annotation.AnnotationConfigApplicationContext@5ecddf8f, started on Wed Aug 19 00:10:13 CST 2020
说明正确的输出告终果信息。
XxxAware的底层原理是由XxxAwareProcessor类实现的, 例如,咱们这里以ApplicationContextAware接口为例,ApplicationContextAware接口的底层原理就是由ApplicationContextAwareProcessor类实现的。从ApplicationContextAwareProcessor类的源码能够看出,其实现了BeanPostProcessor接口,本质上都是后置处理器。
class ApplicationContextAwareProcessor implements BeanPostProcessor
接下来,咱们就以分析ApplicationContextAware接口的原理为例,看看Spring是怎么将ApplicationContext对象注入到Blue类中的。
咱们在Blue类的setApplicationContext()方法上打一个断点,以下所示。
接下来,咱们以debug的方式来运行AutowiredTest类的testAutowired02()方法,
这里,咱们能够看到,实际上ApplicationContext对象已经注入到Blue类中的setApplicationContext()方法中了。咱们在IDEA的方法调用栈中选择postProcessBeforeInitialization()方法,以下所示。
咱们双击IDEA中的postProcessBeforeInitialization()方法的调用栈,会在IDEA中自动定位到postProcessBeforeInitialization()方法中,以下所示。
其实,postProcessBeforeInitialization()方法所在的类就是ApplicationContextAwareProcessor。postProcessBeforeInitialization()方法的逻辑比较简单。
咱们来看下在postProcessBeforeInitialization()方法中调用的invokeAwareInterfaces()方法,以下所示。
看到这里,你们是否是有种豁然开朗的感受!Blue类实现了ApplicationContextAware接口后,Spring为啥会将ApplicationContext对象自动注入到setApplicationContext()方法中就不用说了吧!
其实就是这么简单!
关注「 冰河技术 」微信公众号,后台回复 “设计模式” 关键字领取《深刻浅出Java 23种设计模式》PDF文档。回复“Java8”关键字领取《Java8新特性教程》PDF文档。回复“限流”关键字获取《亿级流量下的分布式限流解决方案》PDF文档,三本PDF均是由冰河原创并整理的超硬核教程,面试必备!!
好了,今天就聊到这儿吧!别忘了点个赞,给个在看和转发,让更多的人看到,一块儿学习,一块儿进步!!
若是你以为冰河写的还不错,请微信搜索并关注「 冰河技术 」微信公众号,跟冰河学习高并发、分布式、微服务、大数据、互联网和云原生技术,「 冰河技术 」微信公众号更新了大量技术专题,每一篇技术文章干货满满!很多读者已经经过阅读「 冰河技术 」微信公众号文章,吊打面试官,成功跳槽到大厂;也有很多读者实现了技术上的飞跃,成为公司的技术骨干!若是你也想像他们同样提高本身的能力,实现技术能力的飞跃,进大厂,升职加薪,那就关注「 冰河技术 」微信公众号吧,天天更新超硬核技术干货,让你对如何提高技术能力再也不迷茫!