在Spring应用中创建全局获取ApplicationContext对象

1、需要创建一个类,实现接口ApplicationContextAware的setApplicationContext方法。

2、在创建的这个类中保存一个静态的ApplicationContext对象,然后通过静态的方法返回。

如下,下面是SpringSide的实现,供参考:

/**

* Copyright (c) 2005-2012 springside.org.cn

*

* Licensed under the Apache License, Version 2.0 (the "License");

*/

package org.springside.modules.test.spring;
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.

  • @author calvin


    */


    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, 自动转型为所赋值对象的类型.


      */


      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.");


      }


      }

在Spring应用中创建全局获取ApplicationContext对象的更多相关文章

  1. spring mvc中几种获取request对象的方式

    在使用spring进行web开发的时候,优势会用到request对象,用来获取访问ip.请求头信息等 这里收集几种获取request对象的方式 方法一:在controller里面的加参数 public ...

  2. 获取applicationContext对象的方法

    方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext(&quo ...

  3. spring获取ApplicationContext对象的方法——ApplicationContextAware

    一. 引言 工作之余,在看一下当年学的spring时,感觉我们以前都是通过get~ set~方法去取spring的Ioc取bean,今天就想能不能换种模型呢?因为我们在整合s2sh时,也许有那么一天就 ...

  4. SpringBoot项目中获取applicationContext对象

    ApplicationContext 对象是Spring开源框架的上下文对象实例,也就是我们常说的Spring容器,一般情况下我们是不用手动来管理它,而是由Spring框架自己来维护bean之间的关系 ...

  5. springMVC 使用WebApplicationContext获取ApplicationContext对象

    主要用于从application中获取bean 1.applicationContext 在web.xml中使用listener配置 <context-param> <param-n ...

  6. Java中创建(实例化)对象的五种方式

    Java中创建(实例化)对象的五种方式1.用new语句创建对象,这是最常见的创建对象的方法. 2.通过工厂方法返回对象,如:String str = String.valueOf(23); 3.运用反 ...

  7. spring 框架通过new Object()获取applicationContext 中的bean方案

    工作中,需要手动创建一个对象,但用到了spring容器中对象的业务逻辑,这时候就要去通过获取容器中的对象.这时候,可以通过实例化一个实现了ApplicationContextAware接口的类获取sp ...

  8. spring中获取ApplicationContext对象的技巧,含源码说明

    第一步,实现接口ApplicationContextAware,重写setApplicationContext方法,下方代码标红的地方,绿色部分 可以通过声明来进行存储到本类中. @Component ...

  9. Spring Boot2 系列教程(十三)Spring Boot 中的全局异常处理

    在 Spring Boot 项目中 ,异常统一处理,可以使用 Spring 中 @ControllerAdvice 来统一处理,也可以自己来定义异常处理方案.Spring Boot 中,对异常的处理有 ...

随机推荐

  1. 「题解」:[组合数学][DP]:地精部落

    拿到这道题秒懂题意:波动序列. 然鹅不会打.想了一节课,想打纯组合数学,结果找不到规律. 想的是先假设拍出一个序列,然后交换其中的元素求组合, 无奈没啥规律可循,显然不能一口气求出来(我说的是我没办法 ...

  2. groupBy 后附加数量和每组百分比

    SELECT i_State, n, , ) rat FROM ( SELECT * FROM ( ) n FROM planinfo GROUP BY i_State ) t1 ) s ) t

  3. AppScan的基础使用

    AppScan是用于Web项目的安全测试工具,扫描网站所有url,自动测试是否存在各种类型的漏洞.AppScan安装在Windows环境上,版本越高,规则库越安全,扫描越全面.   1. 打开AppS ...

  4. maven 项目在 tomcat 中启动报错:Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)

    问题原因: 在下载 maven 依赖包的时候出现某种原因导致下载的依赖包出现损坏,jvm 和 maven 不能正常识别,从而导致出现该问题. 解决办法: 在 maven 仓库中搜索: in-progr ...

  5. mapreduce.Job: Running job: job_1553100392548_0001

    这几天一直在尝试一个mapreduce的例子,但是一直都是卡在mapreduce.Job: Running job: job_1553100392548_0001,查看日志也不报错,查看每个配置文件没 ...

  6. [编织消息框架][netty源码分析]11 ByteBuf 实现类UnpooledHeapByteBuf职责与实现

    每种ByteBuf都有相应的分配器ByteBufAllocator,类似工厂模式.我们先学习UnpooledHeapByteBuf与其对应的分配器UnpooledByteBufAllocator 如何 ...

  7. Elasticsearch系列(一)--入门

    Elasticsearch基于Lucene构建的开源搜索引擎,Java编写,提供restful API,支持横向拓展,能够完成海量数据处理. 应用场景: 1.海量数据分析引擎 2.站内搜索引擎 3.数 ...

  8. wsoj「G2016 SCOI2018 Round #12」建筑师

    传送门 小半个月前的测试,现在翻出来. 考试时我和sxyA了这题. 当时随便搞了个dp,dp[i][j]表示i个数能看到j个的情况数,考虑新加入一个比之前i-1个数都小的数,能看到它的情况是它加到第一 ...

  9. linux命令行操作mysql数据库明细

    连接数据库==> mysql -uroot -p 输入root密码 进入mysql操作后 下面的命令不要忘了最后结尾的; 1.选择数据库命令: use <数据库名> 2.查看表的引擎 ...

  10. 通过原生JS打印一个空心菱形图案

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...