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

时间:2025-02-18 10:12:57

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

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

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

ApplicationContext ap =(servletContextParam)

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

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

ApplicationContext ap = new ClassPathXmlApplicationContext("");

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

    代码: 

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

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

<bean  class="" lazy-init="false"/>

   SpringContextHolder实现代码如下:

package ;
import ;
import ;

import ;
import ;
import ;
import ;
/**
 * 实现对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) {
	 = applicationContext; // NOSONAR
    }

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

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

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

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

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

    public static void clean(){
        ();
    }

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

    
   }

总结:方式1要依赖Servlet容器,方式2实际只适合测试使用,方式1,2都有明显弊端。建议使用方式3。