Spring @PostConstructと@PreDestroyの例
Springでは、
InitializingBean and DisposableBean
インターフェイスを実装するか、リンクを指定することができます://spring/spring-init-method-and-destroy-method-例/[init-methodとdestroy-method]は、初期化と破壊のコールバック関数用のBean設定ファイルにあります。この記事では、アノテーション
@ PostConstruct
と
@ PreDestroy
を使用して同じことを行う方法を説明します。
@PostConstructと@PreDestroy
@PostConstructと@PreDestroyアノテーションを持つCustomerService Bean
package com.mkyong.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アノテーションを認識しません。これを有効にするには、Bean設定ファイルで ‘
CommonAnnotationBeanPostProcessor
‘を登録するか、 ‘
<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.mkyong.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.mkyong.customer.services.CustomerService">
<property name="message" value="i'm property message"/>
</bean>
</beans>
それを実行します
package com.mkyong.common;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.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.mkyong.customer.services.CustomerService@47393f ... INFO:org.springframework.beans.factoryのシングルトンを破棄する support.DefaultListableBeanFactory@77158a: 豆を定義する[customerService];工場階層の根 春のコンテナは破壊されています!顧客のクリーンアップ
-
initIt()メソッド(@PostConstruct)
がメッセージの後に呼び出されます。
プロパティが設定され、
cleanUp()メソッド(@PreDestroy)** はafter after call
context.close();
===ソースコードをダウンロードする
それをダウンロードする –
Spring-PostConstruct-PreDestroy-Example.zip
spring