获取Spring上下文(ApplicationContext)的三种方法

       以前在项目中经常用到Spring上下文(ApplicationContext),每次使用都是百度一下,使用过就忘了。今天良心发现,写一篇博客,让这个知识真正属于我,也希望我写的博文,可以帮助需要的人。

       Spring上下文(ApplicationContext)的获取有三种方式。

   1.通过WebApplicationUtils工具类获取。WebApplicationUtils类是在Spring框架基础包spring-web-3.2.0. RELEASE.jar(我使用的是3.2.0版的jar包,大家可以去spring官网下载最新版的jar)中的类。使用该方法的必须依赖Servlet容器。 使用方法如下: 

ApplicationContext ap =WebApplicationUtils.getWebApplicationContext(servletContextParam)

     其中servletContextParam是你需要传入的Servlet容器参数。

     2. 通过ClassPathXmlApplicationContext类获取。ClassPathXmlApplicationContext 类是在Spring框架基础包spring-context-3.2.0. RELEASE.jar(我使用的是3.2.0版的jar包,大家可以去spring官网下载最新版的jar)中的类。使用方法如下:

ApplicationContext ap = new ClassPathXmlApplicationContext("applicationContext.xml");

     其中applicationContext.xml文件放在src下面,这样就保证可以读取到该文件。这个XML的作用是集中配置和管理所有Bean。

    applicationContext.xml代码: 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        //中间部分是你自己配置的所有bean
</beans>

  3.创建一个自己的工具类(SpringContextHolder)实现Spring的ApplicationContextAware接口。最后在Spring配置文件中注册你的工具类。配置如下:

<bean id="springContextHolder" class="com.fubo.utils.spring.SpringContextHolder" lazy-init="false"/>

   SpringContextHolder实现代码如下:

package com.fubo.utils.spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
 * 实现对spring context 的管理
 * @author FB
 * @2017年3月29日
 * @上午9:07:27
 * @
 */
public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    /**
     * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
     */
    public void setApplicationContext(ApplicationContext applicationContext) {
	SpringContextHolder.applicationContext = applicationContext; // NOSONAR
    }

    /**
     * 取得存储在静态变量中的ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
	checkApplicationContext();
	return applicationContext;
    }

    /**
     * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
	checkApplicationContext();
	return (T) applicationContext.getBean(name);
    }

    /**
     * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
	checkApplicationContext();
	return (T) applicationContext.getBeansOfType(clazz);
    }

    /**
     * 清除applicationContext静态变量.
     */
    public static void cleanApplicationContext() {
	applicationContext = null;
    }

    private static void checkApplicationContext() {
	if (applicationContext == null) {
	    throw new IllegalStateException(
		    "applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
	}
    }
    
    
    public static void setHttpRequestResponseHolder(HttpServletRequest request, HttpServletResponse response){
        responseThreadLocal.set(response);
        ApplicationContext ap = WebApplicationContextUtils.getWebApplicationContext(null);
    }
    public static HttpServletResponse getHttpResponse(){
       return responseThreadLocal.get();
    }

    public static void clean(){
        responseThreadLocal.remove();
    }

    private static final ThreadLocal<HttpServletResponse> responseThreadLocal = new ThreadLocal();

    
   }

总结:方式1要依赖Servlet容器,方式2实际只适合测试使用,方式1,2都有明显弊端。建议使用方式3。
  • 26
    点赞
  • 133
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值