Spring 专题
专题目录
您的位置:java > Spring专题 > Spring @PostConstruct和@PreDestroy实例
Spring @PostConstruct和@PreDestroy实例
作者:--    发布时间:2019-11-20
在spring中,既可以实现 initializingbean和disposablebean接口或在bean配置文件中指定 init-method 和 destroy-method 在初始化和销毁回调函数。在这篇文章中,我们将介绍如何使用 @postconstruct 和 @predestroy 注解来做同样的事情。

注:@postconstruct和@predestroy 标注不属于 spring,它是在j2ee库- common-annotations.jar。

@postconstruct 和 @predestroy

一个 customerservice bean使用 @postconstruct 和 @predestroy 注释
package com.h3.customer.services;

import javax.annotation.postconstruct;
import javax.annotation.predestroy;

public class customerservice
{
	string message;
	
	public string getmessage() {
	  return message;
	}

	public void setmessage(string message) {
	  this.message = message;
	}
	
	@postconstruct
	public void initit() throws exception {
	  system.out.println("init method after properties are set : " + message);
	}
	
	@predestroy
	public void cleanup() throws exception {
	  system.out.println("spring container is destroy! customer clean up");
	}
	
}
默认情况下,spring不会意识到@postconstruct和@predestroy注解。要启用它,要么注册“commonannotationbeanpostprocessor”,要么在bean配置文件的<context:annotation-config />‘ 指定,

1. commonannotationbeanpostprocessor

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
	xsi:schemalocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean class="org.springframework.context.annotation.commonannotationbeanpostprocessor" />

	<bean id="customerservice" class="com.h3.customer.services.customerservice">
		<property name="message" value="i'm property message" />
	</bean>
		
</beans>

2. <context:annotation-config />

<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-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<context:annotation-config />

	<bean id="customerservice" class="com.h3.customer.services.customerservice">
		<property name="message" value="i'm property message" />
	</bean>
		
</beans>

执行结果

package com.h3.common;

import org.springframework.context.configurableapplicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

import com.h3.customer.services.customerservice;

public class app 
{
    public static void main( string[] args )
    {
    	configurableapplicationcontext context = 
    	  new classpathxmlapplicationcontext(new string[] {"spring-customer.xml"});
	
    	customerservice cust = (customerservice)context.getbean("customerservice");
    	
    	system.out.println(cust);
    	
    	context.close();
    }
}

输出结果

init method after properties are set : im property message
com.h3.customer.services.customerservice@47393f
...
info: destroying singletons in org.springframework.beans.factory.
support.defaultlistablebeanfactory@77158a: 
defining beans [customerservice]; root of factory hierarchy
spring container is destroy! customer clean up
initit()方法(@postconstruct)被调用时,消息属性设置后 cleanup() 方法(@predestroy)是在context.close()执行后被调用;

下载源代码 – http://pan.baidu.com/s/1qx2w6xi

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册