在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.yiibai.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;}@PostConstructpublic void initIt() throws Exception {System.out.println("Init method after properties are set : " + message);}@PreDestroypublic 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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /><bean id="customerService" class="com.yiibai.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config /><bean id="customerService" class="com.yiibai.customer.services.CustomerService"><property name="message" value="i'm property message" /></bean></beans>

执行结果

package com.yiibai.common;import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yiibai.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.yiibai.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

Spring @PostConstruct和@PreDestroy实例相关推荐

  1. 注解 @PostConstruct 与 @PreDestroy 详解及实例

    简介 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. Java EE5 引入了@PostConstruct和@PreDestroy这两个作用于Ser ...

  2. spring注解( @Autowired、@Qualifier、@Resource、@PostConstruct、@PreDestroy、 @Component、@Scope)-描述的比较清楚

    概述: 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 P ...

  3. @PostConstruct、@PreDestroy注解介绍及Spring中@PostConstruct、constructor、@Autowired的顺序

    @PostConstruct和@PreDestroy @PostConstruct和@PreDestroy注解是Common Annotations中的注解,Common Annotations原本是 ...

  4. 【Spring注解系列08】@PostConstruct与@PreDestroy

    1.@PostConstruct与@PreDestroy @PostConstruct:在bean创建完成并且属性赋值完成:来执行初始化方法 @PreDestroy:在容器销毁bean之前通知我们进行 ...

  5. Spring学习总结(25)——注解@PostConstruct和@PreDestroy使用总结

    @PostConstruct 和 @PreDestroy 这两个都是方法的注解,将注解标注在方法上,该方法就能够在应用启动和销毁时执行, @PostConstruct 注解标注的函数,是在类执行完构造 ...

  6. Spring生命周期注解之@PostConstruct,@PreDestroy

    目录 1. 注解说明 2. 调用过程 3. 调用分析 1. 注解说明 @PostConstruct,@PreDestroy是Java规范JSR-250引入的注解,定义了对象的创建和销毁工作,同一期规范 ...

  7. PostConstruct和PreDestroy失效

    问题:PostConstruct 和 PreDestroy 失效 说明 今天遇到问题,在使用 Spring的两个注解的过程当中,出现了失效的情况. 也就是 @PostConstruct 和 @PreD ...

  8. activiti自己定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义

    注:(1)环境搭建:activiti自己定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建         (2)创建流程模型:activiti自己定义流程之Spr ...

  9. 注解@PostConstruct与@PreDestroy

    从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion):@PostConstruct和@PreDestroy.这两个注解被用来修饰一个非静态的vo ...

最新文章

  1. 云计算时代的数据库运行
  2. 聊一聊我在 B站 上自学编程的经历吧
  3. asp and asp.net trips
  4. 差分隐私 python_主要开发语言为 Python/Rust:微软与哈佛大学合作开源差分隐私平台...
  5. 关于计算机航天生物的想像作文,未来的航天器想象作文750字
  6. vue cli vue 3.x
  7. Mybatis解决jdbc编程的问题以及mybatis与hibernate的不同
  8. 20155213 2016-2017-2《Java程序设计》课程总结
  9. 『PyTorch』第十五弹_torch.nn.Module的属性设置查询
  10. 关爱程序员,从产品经理做起!
  11. 定投计算器和年化利率计算器
  12. spyder pyecharts不显示_飞利浦292E2E评测丨宽屏显示器中的多面手
  13. iOS系统字体大全,iOS系统中所有的字体
  14. boost电路公式详解
  15. https://mp.csdn.net/
  16. 【无标题】**数据库课设:三天完成学生信息管理系统**
  17. 百度网盘linux版本怎么安装
  18. 【转载】提问的智慧(How To Ask Questions The Smart Way)
  19. 如何使用docker容器中的redis
  20. 2018年——幻灭 2019年——重启

热门文章

  1. Qt Creator粘贴和获取代码段
  2. C++检查图是否为二部图的算法(附完整源码)
  3. 标准 C++ I/O
  4. mysql 数据字典 php_php生成mysql数据库数据字典的程序代码
  5. 4.QT中进程操作,线程操作
  6. Myeclipse的使用方法-添加,修改,删除JRE,修改项目中的jre不显示问题
  7. SQL 聚合函数一定要跟group by以及NULL的关系的案例精讲
  8. php 多维数组怎么去重,php多维数组去重,该如何解决
  9. Python mat与array的区别
  10. 全卷积(FCN)论文阅读笔记:Fully Convolutional Networks for Semantic Segmentation