@Slf4j
@Service
public class SpringContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; /**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
} /**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext;
} /**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
return (T) applicationContext.getBean(name);
} /**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(Class<T> requiredType) {
return applicationContext.getBean(requiredType);
} /**
* 清除SpringContextHolder中的ApplicationContext为Null.
*/
public static void clearHolder() {
if (log.isDebugEnabled()) {
log.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
}
applicationContext = null;
} /**
* 发布事件
*
* @param event
*/
public static void publishEvent(ApplicationEvent event) {
if (applicationContext == null) {
return;
}
applicationContext.publishEvent(event);
} /**
* 实现DisposableBean接口, 在Context关闭时清理静态变量.
*/
@Override
public void destroy() {
SpringContextHolder.clearHolder();
} }

最新文章

  1. CSS line-height与vertical-align:baseline
  2. 大部分人都会做错的经典JS闭包面试题
  3. System.StackOverflowException的一个例子(转)
  4. 使用oracle的大数据工具ODCH访问HDFS数据文件
  5. HTML Agility Pack 搭配 ScrapySharp,彻底解除Html解析的痛苦
  6. NEFU 505 超级红与黑 (高斯消元)
  7. GEF - 制作一个简单图形化编辑框架笔记1
  8. Access restriction : The constructor BASE64Decoder() is not accessible due to restriction on required library
  9. ORACLE 重置SEQQUENCE
  10. Cisco 防止SYN Flood 攻击原理
  11. Oracle 11g gateways(透明网关)配置
  12. Android学习笔记(十五)——碎片的生命周期(附源代码)
  13. DataGirdView 编辑项时的验证
  14. Lambda的使用与实战
  15. JavaScript-通过原型继承一个对象
  16. asp.net上传图片,上传图片
  17. VUE环境部署
  18. ECMAscript5 新增数组内函数
  19. NET Core Kestrel部署HTTPS
  20. 关于在VS2008和VS2010中禁用及卸载Visual Assist X的方法研究——转载

热门文章

  1. 《图解设计模式》读书笔记9-1 Flyweight模式
  2. 【ABAP系列】SAP F4搜索帮助的高级版
  3. WPF资源字典的使用
  4. linux下的SSHD被连接端口修改
  5. 安装paramiko的方法
  6. .net core jessetalk资料合集
  7. 国内下载Flutter
  8. Apache Mesos 官方文档 V1.0
  9. AMBS
  10. flashback table