Spring @PostConstruct and @PreDestroy example
来源:互联网 发布:ubuntu中文输入法 编辑:程序博客网 时间:2024/06/05 14:39
Spring @PostConstruct and @PreDestroy example
from: http://www.mkyong.com/spring/spring-postconstruct-and-predestroy-example/
In Spring, you can either implements InitializingBean and DisposableBean interface or specify the init-method and destroy-method in bean configuration file for the initialization and destruction callback function. In this article, we show you how to use annotation @PostConstruct and @PreDestroy to do the same thing.
NoteThe @PostConstruct and @PreDestroy annotation are not belong to Spring, located in the J2ee library – common-annotations.jar.
@PostConstruct and @PreDestroy
A CustomerService bean with @PostConstruct and @PreDestroy annotation
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"); }}
By default, Spring will not aware of the @PostConstruct and @PreDestroy annotation. To enable it, you have to either register CommonAnnotationBeanPostProcessor or specify the < context:annotation-config /> in bean configuration file,
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>
Run it
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(); }}
Output
Init method after properties are set : im property messagecom.mkyong.customer.services.CustomerService@47393f...INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@77158a: defining beans [customerService]; root of factory hierarchySpring Container is destroy! Customer clean up
- The initIt() method (@PostConstruct) is called, after the message property is set;
- and the cleanUp() method (@PreDestroy) is call after the context.close();
- Spring @PostConstruct and @PreDestroy example
- @PostConstruct and @PreDestroy example
- Spring @PostConstruct 和 @PreDestroy
- Spring Annotation(注解) @Scope ,@PostConstruct 和@PreDestroy
- Spring注解之 @PostConstruct和 @PreDestroy
- SPRING-MVC注解@PostConstruct与@PreDestroy讲解及实例
- spring的init-method,destory-method, @PostConstruct,@ PreDestroy, InitializingBean
- @PostConstruct 和 @PreDestroy 使用
- @PostConstruct 和 @PreDestroy
- @PostConstruct 和 @PreDestroy
- @Scope、@PostConstruct、@PreDestroy
- 注解@PostConstruct与@PreDestroy
- 注解@PostConstruct与@PreDestroy
- 注解@PostConstruct与@PreDestroy
- 注解@PostConstruct与@PreDestroy
- 注解@PostConstruct与@PreDestroy
- 注解@PostConstruct与@PreDestroy
- 注解@PostConstruct与@PreDestroy
- mysql主从复制的高可用解决方案
- Python-OpenCV 图像与视频处理
- NVIDIA JETSON 连接九轴IMU传感器(GY-85模块)
- 驱动编程-idt hook--中断描述符表
- 分布式配置管理平台 Disconf
- Spring @PostConstruct and @PreDestroy example
- getdate.php var_dump($arry);
- Points in Rectangle (树状数组)
- 《java入门第一季》之面向对象面试题(面向对象都做了哪些事情)
- android 利用左右滑动手势实现avtivity的跳转
- [CDQ分治] codeforces 669E. Little Artem and Time Machine
- Spark组件之GraphX学习14--TriangleCount实例和分析
- burnside引理
- 使用mktime 函数取得一个日期的时间戳