欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring启动加载程序的几种方法介绍

程序员文章站 2024-03-01 14:04:04
关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过注解@postconstruct 和 @predestroy 方法...

关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种:

第一种:通过注解@postconstruct 和 @predestroy 方法 实现初始化和销毁bean之前进行的操作

import javax.annotation.postconstruct; 
import javax.annotation.predestroy; 
 
public class datainitializer{  
 @postconstruct 
 public void initmethod() throws exception { 
  system.out.println("initmethod 被执行"); 
 } 
 @predestroy 
 public void destroymethod() throws exception { 
  system.out.println("destroymethod 被执行"); 
 } 
} 

第二种是:通过 在xml中定义init-method 和  destory-method方法

public class datainitializer{ 
 public void initmethod() throws exception { 
  system.out.println("initmethod 被执行"); 
 } 
 public void destroymethod() throws exception { 
  system.out.println("destroymethod 被执行"); 
 } 
} 

复制代码 代码如下:

<bean id="datainitializer" class="com.somnus.demo.datainitializer" init-method="initmethod" destory-method="destroymethod"/> 

第三种是: 通过bean实现initializingbean和 disposablebean接口

import org.springframework.beans.factory.disposablebean; 
 
public class datainitializer implements initializingbean,disposablebean{ 
  
 @override 
 public void afterpropertiesset() throws exception { 
  system.out.println("afterpropertiesset 被执行"); 
 } 
  
 @override 
 public void destroy() throws exception { 
  system.out.println("destroy 被执行"); 
 } 
 
} 

 其中第一种和第二种是同一种形式,只不过一种xml配置,另外一种采用注解形式罢了,有很大区别的是第三种,如果同一个bean同时采用两种方式初始化的时候执行某个方法,首先在执行顺序上就会体现出来。

先执行afterpropertiesset(),后执行initmethod()

这里我们看下源码

这方式在spring中是怎么实现的?

通过查看spring的加载bean的源码类(abstractautowirecapablebeanfactory)可看出其中奥妙

abstractautowirecapablebeanfactory类中的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指定的方法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。