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; /**
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext.
*
*
*/ 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() {
logger.debug("清除SpringContextHolder中的ApplicationContext:"
+ applicationContext);
applicationContext = null;
} /**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
// logger.debug("注入ApplicationContext到SpringContextHolder:{}", applicationContext); if (SpringContextHolder.applicationContext != null) {
logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + SpringContextHolder.applicationContext);
} SpringContextHolder.applicationContext = applicationContext; // NOSONAR
} /**
* 实现DisposableBean接口, 在Context关闭时清理静态变量.
*/
@Override
public void destroy() throws Exception {
SpringContextHolder.clearHolder();
} /**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected() {
Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
}
}
   //调用方式
BackPlateService backPlateService = (BackPlateService) SpringContextHolder.getBean("fyNewBackPlateService");
<!-- 获取bean工具注入 -->  
//在applicationContext.xml 注入
<bean id="springContextHolder" class="com.eee.common.utils.SpringContextHolder" lazy-init="false"/>

最新文章

  1. Jquery自定义扩展方法(二)--HTML日历控件
  2. 通过j-interop访问WMI实例代码
  3. java中的内部类小结
  4. 第22章 DLL注入和API拦截(3)
  5. 杭电1005-Number Sequence
  6. 用Grunt搭建基于LESS的前端html开发框架
  7. left (outer) join , right (outer) join, full (outer) join, (inner) join, cross join 区别
  8. C语言 格式说明符
  9. NSStringDrawingOptions
  10. 负载均衡 Lvs nat 模式笔记
  11. PowerShell:因为在此系统上禁止运行脚本
  12. Oracle存储过程的调用和执行
  13. [转]nodejs使用request发送http请求
  14. ConcurrentHashMap实现原理
  15. 移动端FastClick和editor冲突问题。
  16. mac java jdk 安装删除
  17. print()与println()区别
  18. 查看CPU序列号以及系统信息
  19. VirtualBox虚拟机安装ubuntu系统(图文详解)
  20. JAVA使用qrcode生成二维码(带logo/不带logo)

热门文章

  1. CSS-position 属性&amp;元素脱离文档流引发父级边框塌陷问题
  2. Elasticsearch java api操作(二)(Java High Level Rest Client)
  3. 风炫安全WEB安全学习第二十五节课 利用XSS键盘记录
  4. Java并发编程实战(3)- 互斥锁
  5. 修改postman工具的代码生成工具让它锦上添花
  6. NOIP初赛篇——08计算机安全知识
  7. python -c 妙用
  8. 【Java】计算机软件、博客的重要性、编程语言介绍和发展史
  9. Windows程序通用自动更新模块(C#,.NET4.5以上)
  10. sap的内核升级,修补了源代码保护的方式