Spring bean 实现生命周期的三种解决方案

解决方案一:通过XML配置文件实现:(标签bean的属性init-method和destroy-method)

beans.xml:

<beans>
	<bean id="bean" class="org.spring.tutorial.SimpleBean" init-method="init" destroy-method="destroy" />
</beans>
SimpleBean.java:

package org.spring.tutorial;

public class SimpleBean {

	public SimpleBean() {
		System.out.println("SimpleBean called");
	}
	
	public void init() {
		System.out.println("init called");
	}
	
	public void destroy() {
		System.out.println("destroy called");
	}
	
}


MainApp.java:

package org.spring.tutorial;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

	public static void main(String[] args) {
		AbstractApplicationContext context 
		= new ClassPathXmlApplicationContext("beans.xml"); // 加载指定XML文件的定义
		context.registerShutdownHook();	// 注册关闭钩子
	}
}

运行之:

SimpleBean called
init called
五月 19, 2013 5:59:18 下午 org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1286c71: startup date [Sun May 19 17:59:18 CST 2013]; root of context hierarchy
destroy called

解决方案二:实现Spring的InitializingBean和DisposableBean。

SimpleBean.java:

package org.spring.tutorial;


import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;


public class SimpleBean implements InitializingBean,DisposableBean {


	public SimpleBean() {
		System.out.println("SimpleBean called");
	}


	@Override
	public void destroy() throws Exception {
		System.out.println("destroy called");
	}


	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("afterPropertiesSet called");
	}
	


	
}

beans.xml:

<beans>

	<bean id="bean" class="org.spring.tutorial.SimpleBean" />
	
</beans>

运行结果:

SimpleBean called
afterPropertiesSet called
五月 19, 2013 6:03:44 下午 org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@e77ca4: startup date [Sun May 19 18:03:44 CST 2013]; root of context hierarchy
destroy called

解决方案三:利用javax.annotation包的注解PostConstruct和PreDestroy。

SimpleBean.java:

package org.spring.tutorial;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class SimpleBean {

	public SimpleBean() {
		System.out.println("SimpleBean called");
	}

	@PreDestroy
	public void destroy() throws Exception {
		System.out.println("destroy called");
	}

	@PostConstruct
	public void afterPropertiesSet() throws Exception {
		System.out.println("afterPropertiesSet called");
	}
	

	
}

beans.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />
	<bean id="bean" class="org.spring.tutorial.SimpleBean" />
	
</beans>

运行结果:

SimpleBean called
五月 19, 2013 6:06:39 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@431425: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,bean,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
afterPropertiesSet called
destroy called

总结:可以利用接口(InitializingBean,DisposableBean),注解(@PreDestroy,@PostConstruct)和XML配置文件(bean的属性init-method,destroy-method)来实现bean的生命周期。可以根据个人的喜好选择不同的方案。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值