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的生命周期。可以根据个人的喜好选择不同的方案。




原文地址:https://www.cnblogs.com/javawebsoa/p/3087394.html