Spring注解@PostConstruct与@PreDestroy
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二种是:通过 在xml中定义init-method 和 destory-method方法 第三种是: 通过bean实现InitializingBean和 DisposableBean接口
下面演示通过 @PostConstruct 和 @PreDestory
第一种:
1:定义相关的实现类:
package com.myapp.core.annotation.init; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; public class PersonService { private String message; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} @PostConstruct
public void init(){
System.out.println("I'm init method using @PostConstrut...."+message);
} @PreDestroy
public void dostory(){
System.out.println("I'm destory method using @PreDestroy....."+message);
} }
2:定义相关的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> --> <context:annotation-config /> <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
<property name="message" value="123"></property>
</bean> </beans>
其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;
测试类:
package com.myapp.core.annotation.init; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("resource/annotation.xml"); PersonService personService = (PersonService)context.getBean("personService"); personService.dostory();
} }
测试结果:
I'm init method using @PostConstrut....123
I'm destory method using @PreDestroy.....123
其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
类来告诉Spring容器采用的 常用 注解配置的方式:
只需要修改配置文件为:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> --> <!-- <context:annotation-config /> --> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="personService" class="com.myapp.core.annotation.init.PersonService">
<property name="message" value="123"></property>
</bean> </beans>
同样可以得到以上测试的输出结果。
----------------------------------------------------------
第二种:
1:在xml中配置 init-method和 destory-method方法
只是定义spring 容器在初始化bean 和容器销毁之前的所做的操作基于xml的配置只是一种方式:
直接上xml中配置文件:
<bean id="personService" class="com.myapp.core.beanscope.PersonService" scope="singleton" init-method="init" destroy-method="cleanUp"> </bean>
定义PersonService类:
package com.myapp.core.beanscope;
public class PersonService {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void init(){
System.out.println("init");
}
// how validate the destory method is a question
public void cleanUp(){
System.out.println("cleanUp");
}
}
相应的测试类:
package com.myapp.core.beanscope; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest {
public static void main(String[] args) { AbstractApplicationContext context =new ClassPathXmlApplicationContext("SpringBeans.xml"); PersonService person = (PersonService)context.getBean("personService"); person.setMessage("hello spring"); PersonService person_new = (PersonService)context.getBean("personService"); System.out.println(person.getMessage());
System.out.println(person_new.getMessage());
context.registerShutdownHook(); }
}
测试结果:
init
hello spring
hello spring
cleanUp
可以看出 init 方法和 clean up方法都已经执行了。
context.registerShutdownHook(); 是一个钩子方法,当jvm关闭退出的时候会调用这个钩子方法,在设计模式之 模板模式中 通过在抽象类中定义这样的钩子方法由实现类进行实现,这里的实现类是AbstractApplicationContext,这是spring 容器优雅关闭的方法。
----------------------------------------------------------
第三种:
1:定义相应类实现InitializingBean ,DisposableBean 接口
package com.myapp.core.annotation.init; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; public class PersonService implements InitializingBean,DisposableBean{ private String message; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} @Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("I'm init method using implements InitializingBean interface...."+message); } @Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("I'm init method using implements DisposableBean interface...."+message); } }
2:定义相应的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> --> <!-- <context:annotation-config /> --> <!-- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="personService" class="com.myapp.core.annotation.init.PersonService">
<property name="message" value="123"></property>
</bean>
--> <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
<property name="message" value="123"></property>
</bean> </beans>
3:测试类:
package com.myapp.core.annotation.init; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("resource/annotation.xml"); PersonService personService = (PersonService)context.getBean("personService"); context.registerShutdownHook();
} }
4:输出测试结果:
I'm init method using implements DisposableBean interface....123
三月 16, 2013 5:06:34 下午 org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@205756: startup date [Sat Mar 16 17:06:30 CST 2013]; root of context hierarchy
I'm init method using implements InitializingBean interface....123
Spring注解@PostConstruct与@PreDestroy的更多相关文章
- 注解@PostConstruct与@PreDestroy 特性说明
简介 Java EE5 引入了@PostConstruct和@PreDestroy这两个作用于Servlet生命周期的注解,实现Bean初始化之前和销毁之前的自定义操作.此文主要说明@PostCons ...
- 注解@PostConstruct与@PreDestroy详解及实例
Java EE5 引入了@PostConstruct和@PreDestroy这两个作用于Servlet生命周期的注解,实现Bean初始化之前和销毁之前的自定义操作.此文主要说明@PostConstru ...
- servlet注解@PostConstruct与@PreDestroy
从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion):@PostConstruct和@PreDestroy.这两个注解被用来修饰一个非静态的vo ...
- 注解@PostConstruct与@PreDestroy讲解及实例
从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion):@PostConstruct和@PreDestroy.这两个注解被用来修饰一个非静态的vo ...
- 【JPA】注解@PostConstruct、@PreDestroy
从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的void()方法,而且这个 ...
- spring注解@postConstruct与constructor与@Autowired的启动顺序
本文链接:https://blog.csdn.net/zpflwy1314/article/details/80797756 @Postcontruct’在依赖注入完成后自动调用,例如要将对象a注入到 ...
- spring注解:@PostConstruct和@PreDestroy
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- Spring学习(11)---JSR-250标准注解之 @Resource、@PostConstruct、@PreDestroy
1)@Resource(JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解) Spring 不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定 ...
- Spring 学习——Spring JSR注解——@Resoure、@PostConstruct、@PreDestroy、@Inject、@Named
JSR 定义:JSR是Java Specification Requests的缩写,意思是Java 规范提案.是指向JCP(Java Community Process)提出新增一个标准化技术规范的正 ...
随机推荐
- AC日记——矩阵交换行 openjudge 1.8 01
01:矩阵交换行 总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个5*5的矩阵(数学上,一个r×c的矩阵是一个由r行c列元素排列成的矩形阵列),将第n行和第m行交换,输出交换 ...
- IOS第四课——Autolayout_View
这节课,我们要学习MVC.Selector.Access Control.Extension.Auto Layout.Delegate-Protocol.Custom View. Auto Layou ...
- tween.js
简要教程 tween.js是一款可生成平滑动画效果的js动画库.相关的动画库插件还有:snabbt.js 强大的jQuery动画库插件和Tweene-超级强大的jQuery动画代理插件. tween ...
- MySQL 的乐观并发控制Optimistic concurrency control
默认情况下, MySQL的Innodb事务隔离级别是重复读 repeatable read, SELECT @@GLOBAL.tx_isolation, @@tx_isolation;REPEATAB ...
- Ubuntu终端命令行不显示颜色
在网上找到的一个有效方案是在.bash_profile 中增加颜色定义 export LS_COLORS='di=01;35:ln=01;36:pi=40;33:so=01;35:do=01;35:b ...
- Permutation test 置换检验
来源:Public Library of Bioinformatics 显著性检验通常可以告诉我们一个观测值是否是有效的,例如检测两组样本均值差异的假设检验可以告诉我们这两组样本的均值是否相等(或者那 ...
- 用Myisamchk让MySQL数据表更健康
用Myisamchk让MySQL数据表更健康 2011-03-15 09:15 水太深 ITPUB 字号:T | T 为了让MySQL数据库中的数据表“更健康”,就需要对其进行定期体检.在这里笔者推荐 ...
- UML类图归纳
作为一个程序员,掌握UML类图是开发和阅读程序的基础. 转载请注明地址http://www.cnblogs.com/zrtqsk/p/3739288.html,谢谢! 一.基本介绍 UML是一种标准的 ...
- 学习C++.Primer.Plus 6 分支语句和逻辑操作符
||. &&操作符是一个顺序点 < 操作符从左向右结合 ; < age < )//17<age为true, = 1,肯定 < 27.所以为整个条件为tru ...
- 2015-2016-2 《Java程序设计》项目小组博客
2015-2016-2 <Java程序设计>项目小组博客 1451 完+美 java项目 守望先疯 JavaGroup 07_10_20_22 FromBottomToTop L.G.Su ...