Spring常用工具类(ApplicationContextAware、DisposableBean、InitializingBean)
原创作品,出自 “晓风残月xj”
博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/xiaofengcanyuexj)。
由于各种原因,可能存在诸多不足,欢迎斧正!
1、ApplicationContextAware
org.springframework.context.ApplicationContextAware
实现该接口的类,可以在spring容器初始化的时候调用setApplicationContext方法,从而获得ApplicationContext中的所有bean。
- public class SpringUtils implements ApplicationContextAware {
- private static ApplicationContext applicationContext;
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- SpringUtils.applicationContext = applicationContext;
- synchronized (SpringUtils.class) {
- SpringUtils.class.notifyAll();
- }
- }
- private static void checkForInitialized() {
- if (applicationContext == null) {
- synchronized (SpringUtils.class) {
- if (applicationContext == null) {
- try {
- SpringUtils.class.wait();
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- }
- }
- }
- }
- }
- @SuppressWarnings("unchecked")
- public static <T> T bean(String name) {
- checkForInitialized();
- return (T) applicationContext.getBean(name);
- }
- public static <T> T bean(Class<T> clazz) {
- checkForInitialized();
- return applicationContext.getBean(clazz);
- }
- public static <T> Map<String, T> beans(Class<T> clazz) {
- checkForInitialized();
- return applicationContext.getBeansOfType(clazz);
- }
- }
public class SpringUtils implements ApplicationContextAware {private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtils.applicationContext = applicationContext;
synchronized (SpringUtils.class) {
SpringUtils.class.notifyAll();
}
} private static void checkForInitialized() {
if (applicationContext == null) {
synchronized (SpringUtils.class) {
if (applicationContext == null) {
try {
SpringUtils.class.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
} @SuppressWarnings("unchecked")
public static <T> T bean(String name) {
checkForInitialized();
return (T) applicationContext.getBean(name);
} public static <T> T bean(Class<T> clazz) {
checkForInitialized();
return applicationContext.getBean(clazz);
} public static <T> Map<String, T> beans(Class<T> clazz) {
checkForInitialized();
return applicationContext.getBeansOfType(clazz);
}
}
2、DisposableBean、InitializingBean
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
接口实现初始化方法和销毁前操作主要有下面3种方法:
2.1、第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作;
2.2、第二种是:通过 在xml中定义init-method 和 destory-method方法;
2.3、第三种是: 通过bean实现InitializingBean和 DisposableBean接口;
- 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);
- }
- }
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); }
}
路漫漫其修远兮,很多时候感觉想法比较幼稚。首先东西比较简单,其次工作也比较忙,还好周末可以抽时间处理这个。由于相关知识积累有限,欢迎大家提意见斧正,在此表示感谢!后续版本会持续更新…
Spring常用工具类(ApplicationContextAware、DisposableBean、InitializingBean)的更多相关文章
- 2015第30周三Spring常用工具类
文件资源操作 文件资源的操作是应用程序中常见的功能,如当上传一个文件后将其保存在特定目录下,从指定地址加载一个配置文件等等.我们一般使用 JDK 的 I/O 处理类完成这些操作,但对于一般的应用程序来 ...
- Spring常用工具类
Spring框架下自带了丰富的工具类,在我们开发时可以简化很多工作: 1.Resource访问文件资源: 具体有: ResourceUtils.getFile(url); FileSystemReso ...
- Spring 常用工具类
1) 请求工具类 org.springframework.web.bind.ServletRequestUtils //取请求参数的整数值: public static Integer getIntP ...
- spring中常用工具类介绍
http://www.cnblogs.com/langtianya/p/3875103.html 文件资源操作 Spring 定义了一个 org.springframework.core.io ...
- 简单了解Spring中常用工具类_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口, ...
- Maven基础&&Spring框架阶段常用工具类整理
常用工具类 1.密码加密工具类: package com.itheima.utils; import java.security.MessageDigest; import sun.misc.BASE ...
- commons-lang3-3.2.jar中的常用工具类的使用
这个包中的很多工具类可以简化我们的操作,在这里简单的研究其中的几个工具类的使用. 1.StringUtils工具类 可以判断是否是空串,是否为null,默认值设置等操作: /** * StringUt ...
- commons-lang常用工具类StringEscapeUtils使用--转
https://my.oschina.net/ydsakyclguozi/blog/341496 在apache commons-lang(2.3以上版本)中为我们提供了一个方便做转义的工具类,主要是 ...
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
随机推荐
- FZU 1608 Huge Mission
Huge Mission Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on FZU. Original I ...
- Oracle11gR2 Windows 7 64bit and PL/SQL Developer排错
1.Environment variable: "PATH" - This test checks whether the length of the environment va ...
- ONVIF客户端搜索设备获取rtsp地址开发笔记(精华篇)
原文 http://blog.csdn.net/gubenpeiyuan/article/details/25618177 概要: 目前ONVIF协议家族设备已占据数字监控行业半壁江山以上,亲, ...
- 解决XCODE配置LLVM环境出现的问题
问题描写叙述:在LLVM整合进XCODE的过程中出现符号没有定义导致出现未决函数或方法.但使用终端编译链接生成程序没有问题. 问题产生原因:未引用响应的LLVM库与系统库,以及编译器设置.连接器设置不 ...
- 文字记录而已!!人民币直充/兑换PayPal美金
人民币直充/兑换PayPal美金 用PAYPAL国际使用外国货币,没有信用卡是不能冲值的,到淘宝买吧,被骗的几率大一些,弄来弄去,PAYPAL被冻结. 今天朋友介绍使用 中美互动网 让它给自己的PAY ...
- Vijos 1164 曹冲养猪(中国剩余定理)
P1164曹冲养猪 Accepted 标签:三国争霸[显示标签] 描写叙述 自从曹冲搞定了大象以后,曹操就開始捉摸让儿子干些事业,于是派他到中原养猪场养猪,但是曹冲满不高兴.于是在工作中马马虎虎,有一 ...
- C++ 多线程阻塞 (多线程同步)(MsgWaitForMultipleObjects)(连着消息一起控制,牛)
在主线程定要禁止使用waitforsingleobject(),原因是会阻塞主线程的消息循环,所以必须使用另一种 MsgWaitForMultipleObjects,即可以让消息通过,下面就是一个基于 ...
- 目标识别(object detection)中的 IoU(Intersection over Union)
首先直观上来看 IoU 的计算公式: 由上述图示可知,IoU 的计算综合考虑了交集和并集,如何使得 IoU 最大,需要满足,更大的重叠区域,更小的不重叠的区域. 两个矩形窗格分别表示: 左上点.右下点 ...
- express,中间件(body-parser),req.body获取不到参数(含postman发请求的方法)
问题描述: 最近在做毕设,express 里边的中间件(body-parser)失效,req.body获取不到任何值,req.query能获取到值.一开始加body-parser中间件是有用的,直到昨 ...
- 汉字的ascii