通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
关于在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>
同样可以得到以上测试的输出结果。
通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作的更多相关文章
- Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- Spring实现初始化和销毁bean之前进行的操作,三种方式
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 三种不同实现初始化和销毁bean之前进行的操作的比较
Spring容器中的bean是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种: 通过实现 InitializingBean/ ...
- 在spring容器中定义初始化和销毁bean前所做的操作,有三种方式
1.使用注解,通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 package com.luoq.test.annotation.init; ...
- spring容器初始化bean和销毁bean之前进行一些操作的定义方法
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种,通过在xml中定义init-method和destory-method方法 第二种, ...
- Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean
Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean 七千字长文深刻解读,Spirng中是如何初始化单例bean的,和面试中最常问的Sprin ...
- spring实战三装配bean之Bean的作用域以及初始化和销毁Bean
1.Bean的作用域 所有的spring bean默认都是单例.当容器分配一个Bean时,不论是通过装配还是调用容器的getBean()方法,它总是返回Bean的同一个实例.有时候需要每次请求时都获得 ...
- Spring学习笔记--初始化和销毁Bean
可以使用bean的init-method和destroy-method属性来初始化和销毁bean.定义一个Hero类: package com.moonlit.myspring; public cla ...
随机推荐
- css优化篇
平时总说如何如何优化,今天就详细的写一下css如何优化,嘿嘿. 首先,CSS的优化工作主要从两个方面着手 网络性能:把CSS写到字节数最少,加快下载速度,自然可以让页面渲染的更快一些 语法性能:同样都 ...
- [MCSM]Exponential family: 指数分布族
Exponential family(指数分布族)是一个经常出现的概念,但是对其定义并不是特别的清晰,今天好好看了看WIKI上的内容,有了一个大致的了解,先和大家分享下.本文基本是WIKI上部分内容的 ...
- Sublime Text 之运行 ES6 (基于babel)
本文同步自我的个人博客:http://www.52cik.com/2015/10/21/sublime-text-run-es6.html 之前在博客园里写过一篇<Sublime Text 之运 ...
- 在 Area 中使用RouteAttribute 定义路由, 并支持多语言
业务上的一个需求, 同一页面, 两种不同的使用方法, 为了区分这两种需求, 需要加一个参数到 URL 中,不改路由的话, 是这样: http://localhost:16269/en-US/Forwa ...
- GnuDIP制作动态域名服务器(DDNS Server)_转载http://blog.sina.com.cn/s/blog_4d4c23530100rlfj.html
这个阶段在做DDNS,虽然有dyndns和tzo两个免费的国外的DDNS服务器(支持免费用户注册使用),但是公司需求中要有GnuDIP这种服务.于是只能自己制作DDNS服务器,颇费功夫,于是想把这段记 ...
- [USACO2003][poj2110]Mountain Walking(二分答案+bfs)
http://poj.org/problem?id=2110 题意:给你一个n*n矩形(n<=100),每个位置上都有一个数字代表此处山的高度,要从(1,1)走到 (n,n),要求一条路径使得这 ...
- HDU 5976 Detachment 打表找规律
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5976 Detachment Time Limit: 4000/2000 MS (Java/Other ...
- 如何在Dreamweaver中使用emmet
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=3666 一.emmet ...
- ASP.NET MVC实现POST方式的Redirect
我们知道,在ASP.NET MVC中,要从一个Action跳转到另一个Action,通常是用一系列以“Redirect”开头的方法 Redirect RedirectToAction Redirect ...
- [转]java基础学习总结——equals方法
一.equals方法介绍 1.1.通过下面的例子掌握equals的用法 1 package cn.galc.test; 2 3 public class TestEquals { 4 public s ...