实现 初始化方法和销毁方法3种方式:

  1. 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高)
  2. 设置bean属性 Init-method destroy-method
  3. 使用注释配置后,调用@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;
} @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.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/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.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学习(十二)-----Spring @PostConstruct和@PreDestroy实例的更多相关文章

  1. Spring学习(十二)-----Spring Bean init-method 和 destroy-method实例

    实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...

  2. Spring学习十四----------Spring AOP实例

    © 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

  3. spring学习 十二 AspectJ-based的通知入门 带参数的通知

    第一步:编写通知类 package com.airplan.pojo; import org.aspectj.lang.ProceedingJoinPoint; public class Advice ...

  4. Spring学习十二----------Bean的配置之@ImportResource和@Value

    © 版权声明:本文为博主原创文章,转载请注明出处 @ImportResource -引入XML配置文件 @Value -从配置文件中获取值 实例 1.项目结构 2.pom.xml <projec ...

  5. spring cloud深入学习(十二)-----Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式

    Zuul的核心 Filter是Zuul的核心,用来实现对外服务的控制.Filter的生命周期有4个,分别是“PRE”.“ROUTING”.“POST”.“ERROR”,整个生命周期可以用下图来表示. ...

  6. spring学习十二 application/x-www-form-urlencoded还是application/json

    application/x-www-form-urlencoded还是application/json get. POST 用哪种格式? 后台如何得到这些值? 如何用ajax  或者是 postman ...

  7. spring学习 十五 spring的自动注入

    一  :在 Spring 配置文件中对象名和 ref=”id” ,id 名相同使用自动注入,可以不配置<property/>,对应的注解@Autowired的作用 二: 两种配置办法 (1 ...

  8. Spring学习(十九)----- Spring与WEB容器整合

    首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出的结论是:listener -> ...

  9. Spring学习(十九)----- Spring的五种事务配置详解

    前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...

随机推荐

  1. Odoo中如何复制有唯一性约束的记录?

    转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9281393.html  如果为模型的字段添加了唯一性约束,那么在记录的form视图功能菜单上选择“复制”时就会 ...

  2. selenium + python自动化测试unittest框架学习(三)webdriver元素定位(一)

    1.Webdriver原理 webdirver是一款web自动化操作工具,为浏览器提供统一的webdriver接口,由client也就是我们的测试脚本提交请求,remote server浏览器进行响应 ...

  3. valgrind massif内存分析[转]

    valgrind检查内存泄露 #valgrind   ./程序 内存泄漏问题,我们有memcheck工具来检查.很爽.但是有时候memcheck工具查了没泄漏,程序一跑,内存还是狂飙.这又是什么问题. ...

  4. PHP面试系列 之Linux(二)---- Linux系统定时任务

    环境:ubuntu 16 一.cron实现定时任务 cron实现的定时任务是周期性循环执行的. 1.安装cron sudo apt-get install cron 2.添加定时任务(进行编辑) cr ...

  5. Java Web 程序猿的职业进阶之路

    啥也不说了,都在图里了.希望能够给大家的职业规划一些提示,尤其是写了几年程序,却越来越迷茫的同学.

  6. 获取并安装XWAF框架压缩包(2)

    建议在Eclipse环境下使用XWAF框架来开发用户的Web项目,并遵循以下步骤和约定. 1.获取XWAF框架压缩包文件 程序员点击下列地址免费下载XWAF框架的压缩包文件:XWAF框架压缩文件 2. ...

  7. 【OC底层】OC对象本质,如 isa, super-class

    Objective-C的本质 1.我们编写的Objective-C,底层现实都是C/C++,代码生成步骤如下:   2.在OC中的所有面向对象的实现,都是基于C/C++的数据结构实现的 3.将Obje ...

  8. Python 分支、循环、条件、枚举

    对于表达式,分为“左结合”和“右结合” 左结合:对于没有 = 号的,从左到右边,当然要考虑优先级. 右结合:对于有 = 号存在的情况,右边的自成一体,然后赋值给左边变量   优先级: 逻辑运算符的优先 ...

  9. Java中的引用:强引用、软引用、弱引用、幻象引用(虚引用)

    Java语言中,除了原始数据类型的变量(八大基本数据类型),其他都是引用类型,指向各种不同的对象. 理解引用对于我们掌握Java对象生命周期和JVM内部相关机制都是有帮助的. 不同的应用类型,不同之处 ...

  10. #leetcode刷题之路23-合并K个排序链表

    合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例:输入:[ 1->4->5, 1->3->4, 2->6]输出: 1->1->2- ...