当前位置: 移动技术网 > IT编程>开发语言>Java > Spring中的InitializingBean接口的使用

Spring中的InitializingBean接口的使用

2019年07月19日  | 移动技术网IT编程  | 我要评论

initializingbean接口为bean提供了初始化方法的方式,它只包括afterpropertiesset方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法。

测试,如下:

import org.springframework.beans.factory.initializingbean;
public class testinitializingbean implements initializingbean{
  @override
  public void afterpropertiesset() throws exception {
    system.out.println("ceshi initializingbean");    
  }
  public void testinit(){
    system.out.println("ceshi init-method");    
  }
}

配置文件

<bean id="testinitializingbean" class="com.testinitializingbean" ></bean>

main函数如下

public class main {
  public static void main(string[] args){
    applicationcontext context = new filesystemxmlapplicationcontext("/src/main/java/com/beans.xml");
  }
}

测试结果为:

ceshi initializingbean

这说明在spring初始化bean的时候,如果bean实现了initializingbean接口,会自动调用afterpropertiesset方法。

那么问题来了,在配置bean的时候使用init-method配置也可以为bean配置初始化方法,那这两个哪个会先执行呢,接下来测试一下,修改配置文件,加上init-method:

<bean id="testinitializingbean" class="com.testinitializingbean" init-method="testinit"></bean>

运行程序,得出结果:

ceshi initializingbean
ceshi init-method

从结果可以看出,在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-method
    if (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指定的方法。

spring initializingbean的作用

spring的initializingbean接口有很好的用处,位于spring beans中,它只提供一个方法afterpropertiesset(),当你实现了该方法后,spring就会对你提供框架级的支持:当你通过sring容器生产出实现了该接口的类的实例后,它就会调用afterpropertiesset方法,通过这个方法,你可以检查你的bean是否正确地被初始化了.当然,你也可以用init-method方法.这两种方式可以同时使用,调用的顺序为init-method后调用.

总结

以上所述是小编给大家介绍的spring中的initializingbean接口的使用,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网