spring bean的初始化和销毁的三种方式

1、初始化和销毁bean的第一种方式:
 第一步:在XML文件中创建的bean中添加init-method和destroy-method配置项
 第二步:在XML文件中创建init-method和destroy-method配置项所对应值(即方法名)的方法
 如下面的start()和stop()方法

import org.springframework.beans.factory.DisposableBean;

import org.springframework.beans.factory.InitializingBean;

public class BeanLifeCycle{

   public void start(){//IOC容器初始化时会调用start方法
   System.out.println("bean start:");
   }
   public void stop(){//IOC容器销毁的时候就会调用stop方法
  System.out.println("bean stop");
  }

}

//XML文件如下所示

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<bean id="beanLifeCycle" class="com.nefu.dao.impl.BeanLifeCycle" init-method="start" destroy-method="stop"></bean>
</beans>

 

2、初始化和销毁bean的第二种方式:

第一步:继承InitializingBean,DisposableBean接口,
第二步:实现InitializingBean接口中的afterPropertiesSet方法,用于初始化bean
实现DisposableBean接口中的destroy方法,用于销毁bean

import org.springframework.beans.factory.DisposableBean;

import org.springframework.beans.factory.InitializingBean;

public class BeanLifeCycle implements InitializingBean,DisposableBean{

  @Override
  public void destroy() throws Exception {//IOC容器销毁的时候就会调用destroy方法
    System.out.println("Bean destroy");
  }

  @Override
  public void afterPropertiesSet() throws Exception {//IOC容器初始化时会调用afterPropertiesSet方法
    System.out.println("Bean afterPropertiesSet");
  }

}

//XML文件如下所示

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<bean id="beanLifeCycle" class="com.nefu.dao.impl.BeanLifeCycle" ></bean>
</beans>

 

3、初始化和销毁bean的第三种方式:

使用默认的初始化和销毁方法

第一步:在XML文件中配置default-init-method="defaultInit" default-destroy-method="defaultDestroy"
第二步:在需要创建bean的class中实现defaultInit()和defaultDestroy()方法

 

import org.springframework.beans.factory.DisposableBean;

import org.springframework.beans.factory.InitializingBean;

public class BeanLifeCycle{

  public void defaultInit(){
    System.out.println("Bean defaultInit.");
  }
  public void defaultDestroy()
  {
    System.out.println("Bean defaultDestroy.");
  }

}

//XML文件如下所示

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"

default-init-method="defaultInit" default-destroy-method="defaultDestroy">

<bean id="beanLifeCycle" class="com.nefu.dao.impl.BeanLifeCycle" ></bean>
</beans>

转载于:https://www.cnblogs.com/shlsm/p/9751292.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值