以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext.

使用方法:SpringContextHolder.getBean(NetVideoDao.class);

package com.sh.csv.util;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; /**
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext.
*
* */
@Service
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware,
DisposableBean { private static ApplicationContext applicationContext = null; private static Logger logger = LoggerFactory
.getLogger(SpringContextHolder.class); /**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
assertContextInjected();
return applicationContext;
} /**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
assertContextInjected();
return (T) applicationContext.getBean(name);
} /**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(Class<T> requiredType) {
assertContextInjected();
return applicationContext.getBean(requiredType);
} /**
* 清除SpringContextHolder中的ApplicationContext为Null.
*/
public static void clearHolder() {
if (logger.isDebugEnabled()) {
logger.debug("清除SpringContextHolder中的ApplicationContext:"
+ applicationContext);
}
applicationContext = null;
} /**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext;
} /**
* 实现DisposableBean接口, 在Context关闭时清理静态变量.
*/
@Override
public void destroy() throws Exception {
SpringContextHolder.clearHolder();
} /**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected() {
Validate.validState(applicationContext != null,
"applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
}
}

最新文章

  1. Linux文件结构及基本文件夹
  2. JAVA:NIO初步了解
  3. DOM编程(每天有学习一点篇)
  4. HTML5和CSS3基础教程(第8版)-读书笔记(2)
  5. 利用SET STATISTICS IO和SET STATISTICS TIME 优化SQL Server查询性能
  6. CCD摄像机与CMOS摄像机区别
  7. 整数矩阵CMO 2102回馈(gauss整数解)
  8. perl 处理文本
  9. Coins HDU - 2844
  10. 浅谈轮播图(原生JavaScript实现)
  11. Go实现海量日志收集系统(四)
  12. harpoxy 配置
  13. CDI feature
  14. 分享vs低版本开发的项目到VS高版本时遇到的4个小问题解决之记录
  15. JDK源码之数组
  16. 庞果英雄会第二届在线编程大赛&#183;线上初赛:AB数
  17. 持续集成环境--Tomcat热部署导致线程泄漏
  18. 实现自己的ls命令
  19. 动态改变UITabBarController的菜单文字
  20. adroid swipeRefreshLayout无法显示进度条的问题

热门文章

  1. 【代码笔记】Java常识性基础补充(二)——数组、ArrayList集合、ASCII编码、 eclipse基础操作、eclipse调试、eclipse项目导入、eclipse快捷键
  2. Android sqlite日期存储
  3. nginx 两台机器 出现退款失败问题
  4. C++:关于委托类
  5. centos aws 修改使用密码ssh登录
  6. OutputStream-InputStream-FileOutputStream-FileInputStream-BufferedOutputStream-BufferedInputStream-四种复制方式-单层文件夹复制
  7. 查询SQL Server 版本信息
  8. angular2 文件上传
  9. 【深入理解JAVA虚拟机】第4部分.程序编译与代码优化.1.编译期优化。这章编译和实战部分没理解通,以后再看。
  10. node-7.2.1 already installed, it&#39;s just not linked