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

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

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

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

  1. /**

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

  3. *

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

  5. */

  6. package org.springside.modules.test.spring;
  7.  
  8. import org.apache.commons.lang3.Validate;


  9. import org.slf4j.Logger;


  10. import org.slf4j.LoggerFactory;


  11. import org.springframework.beans.factory.DisposableBean;


  12. import org.springframework.context.ApplicationContext;


  13. import org.springframework.context.ApplicationContextAware;

  14.  
  15. /**
    • 以静态变量保存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. LUOGU P1342 请柬(最短路)

    传送门 解题思路 又是一道语文题,弄清楚题意之后其实就能想出来了,从1跑一遍最短路,把$dis[n]$加入答案.在建个反图跑一遍最短路,把$dis[n]_$加入最短路就行了.第一遍是去的时候,第二遍是 ...

  2. RabbitMQ代码操作之发消息和序列化机制

    几个自动配置类: 1.RabbitAutoConfiguration2.有自动配置了连接工厂 ConnectionFactory3.RabbitProperties 封装了RabbitMQ的配置4.R ...

  3. c语言学习笔记 - 指针和字符串

    前面学习了字符串是一种字符数组,又知道了指针变量和数组的关系,这里来看一下指针和字符串的关系. #include <stdio.h> int main(void){ char str = ...

  4. Nginx是什么

    Nginx很强大,通常作为反向代理服务器,什么是反向代理服务器?就是客户端发送请求给Nginx ,Nginx收到请求后将请求转发给真正的服务器,然后接受服务器处理的结果,最后发送给客户端.客户端以为N ...

  5. jun引导1.04可以让N3050支持6.2

    1.03引导用在3050可以安装 但是安装后找不到dsm 需要手动插拔电源才可以解决 偶尔还会死机 1.04可以引导3050安装6.2 23739 安装24922正常,但是moments传照片后会死机 ...

  6. 怎么解决VirtualBox无法安装增强工具

    点击「设备」-「安装增强功能」,然后就弹出下面这个东西,百度和 bing 了很久,终于解决啦~ Unable to insert the virtual optical disk D:\Program ...

  7. 2019-2-26-SublimeText-快速打开当前文件的文件夹

    title author date CreateTime categories SublimeText 快速打开当前文件的文件夹 lindexi 2019-02-26 18:45:29 +0800 2 ...

  8. Leetcode958. Check Completeness of a Binary Tree二叉树的完全验证性

    给定一个二叉树,确定它是否是一个完全二叉树. 百度百科中对完全二叉树的定义如下: 若设二叉树的深度为 h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集 ...

  9. __autoreleasing 修饰符

    将对象赋值给附有__autoreleasing 修饰符的变量等同于ARC 无效时调用对象的autorelease方法.我们通过以下源代码来看一下. @autoreleasepool { id __au ...

  10. SpringCloud微服务实战三:Hystix的基本概念

    1.说到隔离.熔断.降级,最出名的就是 Netflix 开源的 Hystrix 组件,Hystix官方对它描述为:Hystrix是一个延迟和容错库,旨在隔离远程系统.服务和第三方库,阻止级联故障,在复 ...