Spring学习(十二)-----Spring Bean init-method 和 destroy-method实例
- 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高)
- 设置bean属性 Init-method destroy-method
- 使用注释配置后,调用@PostConstruct和@PreDestroy注解
- 对于init-method方法,它将运行 afterPropertiesSet()在所有的 bean 属性被设置之后。
- 对于destroy-method方法,它将运行 destroy()在 Spring 容器释放该 bean 之后。
示例
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();
}
}
输出
Init method after properties are set : I'm property message
com.yiibai.customer.services.CustomerService@5f49d886
Spring Container is destroy! Customer clean up
Spring学习(十二)-----Spring Bean init-method 和 destroy-method实例的更多相关文章
- Spring学习(十二)-----Spring @PostConstruct和@PreDestroy实例
实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...
- Spring学习(二)--- Bean 作用域
概述 本文介绍的Spring 中bean的作用域. 问题 : bean的作用域有几种,有那些应用场景 bean 装配过程 下图为bean在容器中从创建到销毁的若干阶段. bean 作用域 作用域介绍 ...
- Spring学习十四----------Spring AOP实例
© 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...
- Spring学习(十六)----- Spring AOP实例(Pointcut(切点),Advisor)
在上一个Spring AOP通知的例子,一个类的整个方法被自动拦截.但在大多数情况下,可能只需要一种方式来拦截一个或两个方法,这就是为什么引入'切入点'的原因.它允许你通过它的方法名来拦截方法.另外, ...
- Spring学习(十五)----- Spring AOP通知实例 – Advice
Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...
- Spring学习十二----------Bean的配置之@ImportResource和@Value
© 版权声明:本文为博主原创文章,转载请注明出处 @ImportResource -引入XML配置文件 @Value -从配置文件中获取值 实例 1.项目结构 2.pom.xml <projec ...
- spring学习 十二 AspectJ-based的通知入门 带参数的通知
第一步:编写通知类 package com.airplan.pojo; import org.aspectj.lang.ProceedingJoinPoint; public class Advice ...
- spring cloud深入学习(十二)-----Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
Zuul的核心 Filter是Zuul的核心,用来实现对外服务的控制.Filter的生命周期有4个,分别是“PRE”.“ROUTING”.“POST”.“ERROR”,整个生命周期可以用下图来表示. ...
- 【spring学习笔记二】Bean
### bean的三种实例化方式: 1.构造 2.静态工厂 3.实例工厂 其中,工厂就是工厂的概念,工厂函数factor-method会返回她生产出来的产品类. 而构造初始化也可以选择初始化方式和销毁 ...
随机推荐
- ZooKeeper学习之路 (五)ZooKeeper API的简单使用 增删改查
zookeeper文件系统的增删改查 public class ZKDemo1 { private static final String CONNECT_STRING = "hadoop1 ...
- numeric_limits<>函数
因为比较有用,所以自己试验并且翻译了一下,很可能有错误,希望发现错误的朋友能给我留言纠正,谢谢! 有部分没有翻译,因为还没弄清楚到底什么作用 numeric_limits是模板类. 需要注意的是返回值 ...
- 2.4G电动车防盗方案 超低功耗单发器 SI24R2F
对于现在的电动车防盗标签和校园卡的市场,主要以2.4G为主做标签,各色各样的2.4G国产芯片渐渐的能满足这块RFID领域.但是作为RFID的推动领导者,深圳市动能世纪科技有限公司专注于超 ...
- Java 读取properties
package Db; import java.io.InputStream; import java.util.Properties; import java.io.BufferedReader; ...
- struts2的动态方法调用(DMI)和通配符映射
动态方法调用 1.Struts2默认关闭DMI功能,需要使用需要手动打开,配置常量 struts.enable.DynamicMethodInvocation = true 2.使用“!”方法,即 ...
- Project Euler:Problem 86 Cuboid route
A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the o ...
- git add用法
git add命令是将工作区内容添加到暂存区.git commit 将暂存区内容添加到版本库. git add -A 提交所有变化 git add -u 提交被修改(modified)和被删除(d ...
- 分布式架构学习-Consul集群配置
简介 之前公司用的是Consul进行服务发现以及服务管理,自己一直以来只是用一下,但是没有具体的深入,觉得学习不可以这样,所以稍微研究了一下. 网上有很多关于Consul的介绍和对比,我这里也不献丑了 ...
- oracle定时器在项目中的应用
业务需求: 现在业务人员提出了一个需求: 在项目中的工作流,都要有一个流程编号,此编号有一定的规则: 前四五位是流程的字母缩写,中间是8位的日期,后面五位是流水码,要求流水码每天从00001开始.即: ...
- GCD 多线程技术
Grand Central Dispatch(GCD)是异步执行任务的技术之一.一般将应用程序中记述的线程管理用 的代码在系统级中实现.开发者只需要定义想执行的任务并追加到适当的Dispatch Qu ...