Spring学习(十二)-----Spring @PostConstruct和@PreDestroy实例
- 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高)
- 设置bean属性 Init-method destroy-method
- 使用注释配置后,调用@PostConstruct和@PreDestroy注解
注:@PostConstruct和@PreDestroy 标注不属于 Spring,它是在J2EE库- common-annotations.jar。
@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");
} }
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
Spring学习(十二)-----Spring @PostConstruct和@PreDestroy实例的更多相关文章
- Spring学习(十二)-----Spring Bean init-method 和 destroy-method实例
实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...
- Spring学习十四----------Spring AOP实例
© 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...
- spring学习 十二 AspectJ-based的通知入门 带参数的通知
第一步:编写通知类 package com.airplan.pojo; import org.aspectj.lang.ProceedingJoinPoint; public class Advice ...
- Spring学习十二----------Bean的配置之@ImportResource和@Value
© 版权声明:本文为博主原创文章,转载请注明出处 @ImportResource -引入XML配置文件 @Value -从配置文件中获取值 实例 1.项目结构 2.pom.xml <projec ...
- spring cloud深入学习(十二)-----Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
Zuul的核心 Filter是Zuul的核心,用来实现对外服务的控制.Filter的生命周期有4个,分别是“PRE”.“ROUTING”.“POST”.“ERROR”,整个生命周期可以用下图来表示. ...
- spring学习十二 application/x-www-form-urlencoded还是application/json
application/x-www-form-urlencoded还是application/json get. POST 用哪种格式? 后台如何得到这些值? 如何用ajax 或者是 postman ...
- spring学习 十五 spring的自动注入
一 :在 Spring 配置文件中对象名和 ref=”id” ,id 名相同使用自动注入,可以不配置<property/>,对应的注解@Autowired的作用 二: 两种配置办法 (1 ...
- Spring学习(十九)----- Spring与WEB容器整合
首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出的结论是:listener -> ...
- Spring学习(十九)----- Spring的五种事务配置详解
前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...
随机推荐
- notepad++怎样添加文件目录
需要安装一个Explorer.dll文件 方法一:这个方法我个人试了不成功,可能因为版本问题,进入后选择Explorer进行安装 方法二:网上下载 Explorer.dll文件,放到\\No ...
- USDT/BTC/ETC/HT的解释
USDT 泰达币 泰达币(USDT)是Tether公司推出的基于稳定价值货币美元(USD)的代币Tether USD(下称USDT),1USDT=1美元,用户可以随时使用USDT与USD进行1:1兑换 ...
- HttpClient使用小结
使用HttpClient发送请求.接收响应很简单,只要如下几步即可. 1. 创建HttpClient对象. 2. 如果需要发送GET请求,创建HttpGet对象:如果需要发送POST请求,创建Http ...
- UICollectionViewFlowLayout 的 estimatedItemSize 属性
这个是collectionView的item 自适应fram的属性, 介绍在网上很多, 但是用法没有太多的举例, 其实这个属性的使用也很简单, 随便给它的不为CGSizeZero的值就好, 但是, 但 ...
- 网络测量中基于Sketch方法的简单介绍
Sketch介绍 为什么要用Sketch 网络流主要根据五元组.主机地址.包的大小来分类.在网络中存在各种各样的包,如果按照上述分类方法,对每一种包都分配一个计数器来储存,虽然测量准确,那么存放计数器 ...
- cocos2d-x3.0 用CCDictionary写文件
bool CDownLoad_LocalData::WriteToConfigFile( DownLoadLocalData* downdata ){ CCDictionary* pDict = CC ...
- React Native获取组件位置和大小
RN页面中定位或滚动操作时,需要获取元素的大小和位置信息,有几种常用的方法 获取设备屏幕的宽高 import {Dimensions} from 'react-native'; var {height ...
- linux、SMART、Shell的学习
Linux指令 useradd 添加用户 添加用户 -d 添加用户路径 -e 制定密码有效时间 -G -g 指定用户所属组 -c 指定一段用户描述 -s Shell文件指定用户的登录Shell -u ...
- Some cool FireMonkey multi-device components
http://blogs.embarcadero.com/davidi/2014/01/16/43281 There are many available Delphi and C++Builder ...
- MySQL数据库实验:任务一 创建数据库和表
目录 任务一 创建数据库和表 [实训目的与要求] [实训原理] [实训步骤] 一.熟悉MySQL环境 二.利用MySQL命令行窗口创建数据库及表 三.利用界面工具创建数据库及表 任务一 创建数据库和表 ...