通过ApplicationContextAware加载Spring上下文环境

通过ApplicationContextAware加载Spring上下文环境

原创 2013年09月05日 18:15:44

项目用到了ApplicationContextAware,通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法。

我们在ApplicationContextAware的实现类中,就可以通过这个上下文环境对象得到Spring容器中的Bean。

使用方法如下:

1.实现ApplicationContextAware接口:

[java] view plain copy

  1. package com.bis.majian.practice.module.spring.util;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.ApplicationContextAware;
  5. public class SpringContextHelper implements ApplicationContextAware {
  6. private static ApplicationContext context = null;
  7. @Override
  8. public void setApplicationContext(ApplicationContext applicationContext)
  9. throws BeansException {
  10. context = applicationContext;
  11. }
  12. public static Object getBean(String name){
  13. return context.getBean(name);
  14. }
  15. }

2.在Spring的配置文件中配置这个类,Spring容器会在加载完Spring容器后把上下文对象调用这个对象中的setApplicationContext方法:

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/tx
  8. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName">
  11. <bean id="springContextHelper" class="com.bis.majian.practice.module.spring.util.SpringContextHelper"></bean>
  12. <context:component-scan base-package="com.bis.majian.practice.module.*" />
  13. </beans>

3.在web项目中的web.xml中配置加载Spring容器的Listener:

[html] view plain copy

  1. <!-- 初始化Spring容器,让Spring容器随Web应用的启动而自动启动 -->
  2. <listener>
  3. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  4. </listener>

4.在项目中即可通过这个SpringContextHelper调用getBean()方法得到Spring容器中的对象了。

时间: 2024-08-29 13:29:01

通过ApplicationContextAware加载Spring上下文环境的相关文章

如何在Web启动时获取Spring 上下文环境

问题如题,本文给出一个解决方案,满足以下要求: (1)Web启动时能自动获取Spring 的上下文,从而用户可以随意的获取其中的Bean. (2)在单文件测试环境中,亦可以根据配置文件路径加载Spring上下文. 大致原理为: Spring 启动时,会给任何一个实现了ApplicationContextAware接口的Bean(也可以叫做类),注入一个构造函数参数:applicationContext. 有了这样的一个类,就可以在自己的代码里轻松的获取上下文了. 编写这样的一个类还有个好处,那就

【Spring】Junit加载Spring容器作单元测试

如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单元测试. > 基本的搭建 (1)引入所需的包 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test<

Junit加载Spring容器作单元测试

如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单元测试. > 基本的搭建 (1)引入所需的包 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test<

struts加载spring

为了在Struts中加载Spring context,需要在struts-config.xml文件中加入如下部分: <struts-config> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/appli

【Spring】Junit加载Spring容器作单元测试(转)

[Spring]Junit加载Spring容器作单元测试 阅读目录 > 基本的搭建 > 常见的用法 如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单元测试. > 基本的搭建 (1)引入所需的包 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> &

加载spring容器

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * 加载spring容器 **/ public class SpringUtil { public static ApplicationContext context; static{ context = new ClassPa

在web.xml正确加载spring配置文件的方式

ssm框架整合时一直报出没有创建实例bean的错误,一直以为是代码原因,反复测试了很久,才找到原因是spring配置文件没有正确导入,下图是我的错误示例 web.xml加载spring配置文件的方式主要依据该配置文件的名称和存放的位置不同来区别,目前主要有两种方式. 1.如果spring配置文件的名称为applicationContext.xml,并且存放在WEB-INF/目录下,那么只需要在web.xml中加入以下代码即可 <listener> <listener-class>o

加载spring 的方法。

(一)ContextLoaderListener 与 ContextLoaderServlet本质上是等同的,都是调用ContextLoader来加载web程序的上下文, 加载完成以后,都是在ServletContext中,只不过listener需要Servlet2.3及以上支持. (二)ContextLoaderListene与DispatcherServlet的区别: 我们知道spring在web.xml中可以有三种方式来配置其xml路径,来加载Bean:org.springframewor

junit测试用例加载spring配置文件

junit加载pom引用项目的xml配置文件,如果定义了<beans profile="dev">,必须在测试用例类上面加上标记 @ActiveProfiles("product"),否则spring找不到<beans profile="dev">里面的配置.提示错误信息: No bean named '*Mongo' is defined