环境 jdk1.8、springboot、idea
代码案例
- 【方式一】实现接口方式InitializingBean

-
【方式二】通过xml配置方式<bean init-method="myInit" /bean>


spring源码分析
- org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory的invokeInitMethods方法 【自己可以找下】

- 注意这里是直接通过Bean的方法去调用afterPropertiesSet

- 这里是<bean init-method="myInit" /bean>执行的地方



- 通过反射java.lang.reflect.Method的invoke方法去执行

- DisposableBean和 destroy-method="myDestroy" 的源码
- 也是一个Bean方法直接调用,一个反射destroy-method


总结
- Spring 2种Bean初始方法的执行方式,可以同时执行,接口方式InitializingBean先执行,配置init-method方式后执行,详情请见spring源码
- 执行效率分析,接口InitializingBean方式比 通过反射配置init-method方式快。
- 其他特性请debug进去自己看看,源码还是要看看的。
转载自:https://www.toutiao.com/a6617612019585516045/?tt_from=weixin&utm_campaign=client_share&wxshare_count=1×tamp=1541030956&app=news_article&utm_source=weixin&iid=46534582877&utm_medium=toutiao_ios&group_id=6617612019585516045
相关文章:
-
2022-12-23
-
2021-10-06
-
2021-09-21
-
2021-12-09
-
2021-05-19
-
2022-12-23
-
2022-12-23
-
2021-08-19