zoukankan      html  css  js  c++  java
  • 获取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。


    原文:https://blog.csdn.net/fubo1990/article/details/79648766

  • 相关阅读:
    RecyclerView 源码分析(一) —— 绘制流程解析
    sqlserver outer join
    获取最后一个标签 xpath
    pytesseract
    pytesseract代码优化
    sql server recursion
    sql
    sql server
    mssql乱码问题
    SQL
  • 原文地址:https://www.cnblogs.com/anyiz/p/10660845.html
Copyright © 2011-2022 走看看