`
lk557
  • 浏览: 139414 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

 
阅读更多

在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中文帮助文档

    12.2.2. 在Spring容器中创建 SessionFactory 12.2.3. The HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. 声明式的...

    Spring-Reference_zh_CN(Spring中文参考手册)

    12.2.2. 在Spring的application context中创建 SessionFactory 12.2.3. HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. ...

    Spring.html

    ClassPathXmlApplicationContext:使用这个工厂创建对象,他会根据scope智能判断是否懒加载,如果是单例则创建容器时就会创建里面bean的实例,如果是多例在获取使用时才会创建bean实例 ...

    Spring 2.0 开发参考手册

    12.2.2. 在Spring的application context中创建 SessionFactory 12.2.3. HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7...

    Spring API

    12.2.2. 在Spring容器中创建 SessionFactory 12.2.3. The HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. 声明式的...

    spring chm文档

    12.2.2. 在Spring的application context中创建 SessionFactory 12.2.3. HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7...

    +Flex+集成到+Java+EE+应用程序的最佳实践(完整源代码)

    在这个应用程序中,被创建的 FactoryInstance 实际对象是 SpringFactoryInstance; FactoryInstance 的 lookup() 方法被调用,在 SpringFactoryInstance 中,首先查找 Spring 容器,然后,通过 Bean 的 ID 查找 ...

    Spring IoC中的基础知识

    1.IoC的定义 IoC称为控制反转又叫DI(依赖注入)。 2.Spring Spring的定义:是一个IoC容器  分为: spring core  spring beans: 组件...ApplicationContext 与BeanFactory 之间的关系。后面会以代码格式详细说明

    将 Flex 集成到 Java EE 应用程序的最佳实践(完整源代码)

    在这个应用程序中,被创建的 FactoryInstance 实际对象是 SpringFactoryInstance; FactoryInstance 的 lookup() 方法被调用,在 SpringFactoryInstance 中,首先查找 Spring 容器,然后,通过 Bean 的 ID 查找 ...

    spring-boot-template:spring boot项目模板项目,方便快速创建

    基础模板项目 版本 Spring boot 2.2.5 Mybatis Plus 3.4.1 介绍 本项目基于Spring Boot项目重建,实现的功能有: 统一响应结果封装及生成工具 ...整体applicationContext内容的获取 全局控制器层出入参数打印

    java微信公众号MVC开发框架

    web.xml是web应用的配置文件,jwx从spring配置文件中获取配置信息,所以必须配置spring上下文环境;另外,需要配置微信消息处理分发Servlet(WeixinDispatcherServlet),用于处理微信送过来的请求消息或事件。jwx对...

    Faceye 基础版概框

    Ext2.1,Struts1.3,Hibernate3.2,Spring2.5,Acegi 1.06,MySQL 2.功能概述: Blog,RSS订阅,网址导航 3.特色 基于标准的SSH结构,基础Ext的展现界面,基于数据库的acegi安全认证.可随意更改的portal布局,可随意添加的...

    谷歌师兄的leetcode刷题笔记-webda:无服务器Javascript开发框架

    webda.config.json包含了app的配置,定义了Services、Routes,以及全局配置,如果你愿意,可以把它当作Spring的applicationContext.xml,Beans=Services 快速开始 您应该查看我们的演示项目: 查看我们的视频演示 ...

Global site tag (gtag.js) - Google Analytics