欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 前端技术 > javascript >内容正文

javascript

spring中的initializingbean的使用详解 -凯发k8官方网

发布时间:2025/1/21 javascript 15 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 spring中的initializingbean的使用详解 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

      • 一、initializingbean作用
      • 二、initializingbean扩展

写在前面: 我是「境里婆娑」。我还是从前那个少年,没有一丝丝改变,时间只不过是考验,种在心中信念丝毫未减,眼前这个少年,还是最初那张脸,面前再多艰险不退却。
写博客的目的就是分享给大家一起学习交流,如果您对 java感兴趣,可以关注我,我们一起学习。

一、initializingbean作用

initializingbean简介:

initializingbean是spring提供的拓展性接口,initializingbean接口为bean提供了属性初始化后的处理方法,它只有一个afterpropertiesset方法,凡是继承该接口的类,在bean的属性初始化后都会执行该方法。

initializingbean用法
自定义myinitializingbean实现initializingbean接口

@component public class myinitializingbean implements initializingbean {@overridepublic void afterpropertiesset() throws exception {system.out.println("我是启动时加载...");} }

程序启动在日志中看到如下信息:

二、initializingbean扩展

构造方法、注解postconstruct,实现initializingbean方法afterpropertiesset,bean初始化init方法执行顺序。

@component public class myinitializingbean implements initializingbean {public myinitializingbean() {system.out.println("我是myinitializingbean构造方法执行...");}@overridepublic void afterpropertiesset() throws exception {system.out.println("我是afterpropertiesset方法执行...");}@postconstructpublic void postconstruct() {system.out.println("我是postconstruct方法执行...");}public void init(){system.out.println("我是init方法执行...");}@bean(initmethod = "init")public myinitializingbean test() {return new myinitializingbean();} }

程序启动在日志中看到如下信息:

通过启动日志我们可以看出执行顺序优先级:构造方法 > postconstruct >afterpropertiesset > init方法。

在spring初始化bean的时候,如果该bean实现了initializingbean接口,并且同时在配置了init-method,系统则是先调用afterpropertieset()方法,然后再调用init-method中指定的方法。

那么这种方式在spring中是怎么实现的呢,通过查看spring加载bean的源码类abstractautowiredcapablebeanfactory可以看出其中的奥妙,abstractautowiredcapablebeanfactory类中的invokeinitmethods说的非常清楚,如下:

protected void invokeinitmethods(string beanname, final object bean, rootbeandefinition mbd) throws throwable {//判断该bean是否实现了实现了initializingbean接口,如果实现了initializingbean接口,则只掉调用bean的afterpropertiesset方法boolean isinitializingbean = (bean instanceof initializingbean);if (isinitializingbean && (mbd == null || !mbd.isexternallymanagedinitmethod("afterpropertiesset"))) {if (logger.isdebugenabled()) {logger.debug("invoking afterpropertiesset() on bean with name '" beanname "'");}if (system.getsecuritymanager() != null) {try {accesscontroller.doprivileged(new privilegedexceptionaction<object>() {public object run() throws exception {//直接调用afterpropertiesset((initializingbean) bean).afterpropertiesset();return null;}},getaccesscontrolcontext());} catch (privilegedactionexception pae) {throw pae.getexception();}} else {//直接调用afterpropertiesset((initializingbean) bean).afterpropertiesset();}}if (mbd != null) {string initmethodname = mbd.getinitmethodname();//判断是否指定了init-method方法,如果指定了init-method方法,则再调用制定的init-methodif (initmethodname != null && !(isinitializingbean && "afterpropertiesset".equals(initmethodname)) &&!mbd.isexternallymanagedinitmethod(initmethodname)) {//进一步查看该方法的源码,可以发现init-method方法中指定的方法是通过反射实现invokecustominitmethod(beanname, bean, mbd);}} }

总结:

1、spring为bean提供了两种初始化bean的方式,实现initializingbean接口,实现afterpropertiesset方法,或者在配置文件中通过init-method指定,两种方式可以同时使用。

2、实现initializingbean接口是直接调用afterpropertiesset方法,比通过反射调用init-method指定的方法效率要高一点,但是init-method方式消除了对spring的依赖。

3、如果调用afterpropertiesset方法时出错,则不调用init-method指定的方法。

到此initializingbean介绍完毕。如果还有不明白的可以留言。
—————————————————————————————————
由于本人水平有限,难免有不足,恳请各位大佬不吝赐教!

总结

以上是凯发k8官方网为你收集整理的spring中的initializingbean的使用详解的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图