实现 初始化方法和销毁方法3种方式:
  1. 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高)
  2. 设置bean属性 Init-method destroy-method
  3. 使用注释配置后,调用@PostConstruct和@PreDestroy注解
 
在Spring中,可以使用 init-method 和 destroy-method 在bean 配置文件属性用于在bean初始化和销毁某些动作时。这是用来替代 InitializingBean和DisposableBean接口
  • 对于init-method方法,它将运行 afterPropertiesSet()在所有的 bean 属性被设置之后。
  • 对于destroy-method方法,它将运行 destroy()在 Spring 容器释放该 bean 之后。

示例

这里有一个例子向您展示如何使用 init-method 和 destroy-method。
package com.yiibai.customer.services;

public class CustomerService
{
String message; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public void initIt() throws Exception {
System.out.println("Init method after properties are set : " + message);
} public void cleanUp() throws Exception {
System.out.println("Spring Container is destroy! Customer clean up");
} } File : applicationContext.xml, 在bean中定义了init-method和destroy-method属性。 <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 id="customerService" class="com.yiibai.customer.services.CustomerService"
init-method="initIt" destroy-method="cleanUp"> <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[] {"applicationContext.xml"}); CustomerService cust = (CustomerService)context.getBean("customerService"); System.out.println(cust); context.close();
}
}
ConfigurableApplicationContext.close将关闭应用程序上下文,释放所有资源,并销毁所有缓存的单例bean。

输出

Init method after properties are set : I'm property message
com.yiibai.customer.services.CustomerService@5f49d886
Spring Container is destroy! Customer clean up
 initIt()方法被调用,消息属性设置后,在 context.close()调用后,执行 cleanUp()方法;
建议使用init-method 和 destroy-methodbean 在Bena配置文件,而不是执行 InitializingBean 和 DisposableBean 接口,也会造成不必要的耦合代码在Spring。
在Spring中,可以使用 init-method 和 destroy-method 在bean 配置文件属性用于在bean初始化和销毁某些动作时。这是用来替代 InitializingBean和DisposableBean接口
 

Spring学习(十二)-----Spring Bean init-method 和 destroy-method实例的更多相关文章

  1. Spring学习(十二)-----Spring @PostConstruct和@PreDestroy实例

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

  2. Spring学习(二)--- Bean 作用域

    概述 本文介绍的Spring 中bean的作用域. 问题 : bean的作用域有几种,有那些应用场景 bean 装配过程 下图为bean在容器中从创建到销毁的若干阶段. bean 作用域 作用域介绍 ...

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

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

  4. Spring学习(十六)----- Spring AOP实例(Pointcut(切点),Advisor)

    在上一个Spring AOP通知的例子,一个类的整个方法被自动拦截.但在大多数情况下,可能只需要一种方式来拦截一个或两个方法,这就是为什么引入'切入点'的原因.它允许你通过它的方法名来拦截方法.另外, ...

  5. Spring学习(十五)----- Spring AOP通知实例 – Advice

    Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...

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

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

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

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

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

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

  9. 【spring学习笔记二】Bean

    ### bean的三种实例化方式: 1.构造 2.静态工厂 3.实例工厂 其中,工厂就是工厂的概念,工厂函数factor-method会返回她生产出来的产品类. 而构造初始化也可以选择初始化方式和销毁 ...

随机推荐

  1. 马克飞象markdown用法

    目录 markdown用法 ### 根据标题生成目录 `` 快捷键 ctrl+k 代码区域 ctrl+2 二级标题 ctrl+b/i 粗体/斜体 ctrl+l 插入链接 ctrl+g 插入图片 ctr ...

  2. [19/04/30-星期二] GOF23_行为型模式(中介者模式、命令模式、解释器模式、访问者模式)

    一.中介者模式(meditor) [中介] /*** * 抽象中介者接口和其具体实现类"经理"类 */ package cn.sxt.meditor; import java.ut ...

  3. [19/04/18-星期四] Java的动态性_动态编译(DynamicCompiler,Dynamic:动态的,Compiler:编译程序)

    一.概念 应用场景:如在线评测系统,客户端编写代码,上传到服务器端编译运行:服务器动态加载某些类文件进行编译 /*** * */ package cn.sxt.jvm; import java.io. ...

  4. 【JavaScript】富文本编辑器

    这是js写的富文本编辑器,还存在一些bug,但基本功能已经实现,通过这个练习,巩固了js富文本编辑方面的知识,里面包含颜色选择器.全屏.表情.上传图片等功能,每个功能实际对应的就是一个小插件啦 部分程 ...

  5. 【MySQL-123】MySQL8.0.12 安装于Win10

    参考blog:MySQL8.0.12 安装及配置 [坑一]输入net start mysql时,MYSQL服务无法启动. 问题:第三步my.ini文件编码错误. 解决方案:https://blog.c ...

  6. 4、Dubbo-工程实践

    4.实践测试 4.1).提出需求 某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址: 我们现在 需要创建两个服务模块进行测试  测试预期结果: 订单服务web模块在A服务器,用户服务模块在 ...

  7. selenium + python自动化测试unittest框架学习(三)webdriver元素操作(二)

    上一篇是元素的定位,那么定位元素的目的就是对元素进行操作,例如写入文本,点击按钮,拖动等等的操作 (1)简单元素操作 简单元素操作 find_element_by_id("kw") ...

  8. pdo_mysql扩展以及测试

    1.进入 PHP 的软件包 pdo 扩展目录中(注:php的tar包解压目录) 2.配置和编译文件 进入 在PHP源码包中进入 cd /data/php-5.6.36/ext/pdo_mysql 执行 ...

  9. 【luogu P2827 蚯蚓】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2827 35分:暴力sortO(mnlogn). 80分:考虑到每次不好维护不被切的点+q,正难则反.改成维护 ...

  10. Reading SketchVisor Robust Network Measurement for Sofeware Packet Processing

    SIGCOMM17 摘要 在现有的网络测量任务中包括流量监测.数据收集和一系列网络攻击的预防.现有的基于sketch的测量算法存在严重性能损失.大量计算开销以及测量的精确性不足,而基于硬件的优化方法并 ...