zoukankan      html  css  js  c++  java
  • 获取spring上下文

    前言

      spring上下文是spring容器抽象的一种实现。将你需spring帮你管理的对象放入容器的一种对象,ApplicationContext是一维护Bean定义以及对象之间协作关第的高级接口。

    获取spring的上下文环境ApplicationContext的方式

    一)、通过WebApplicationUtils工具类获取。

      WebApplicationUtils类是在Spring框架基础包spring-web-3.2.0. RELEASE.jar中的类。使用该方法的必须依赖Servlet容器。 使用方法如下: 

    // Spring中获取ServletContext对象,普通类中可以这样获取
    ServletContext sc = ContextLoader.getCurrentWebApplicationContext().getServletContext();
    // servlet中可以这样获取,方法比较多
    ServletContext sc = request.getServletContext():
    ServletContext sc = servletConfig.getServletContext();  //servletConfig可以在servlet的init(ServletConfig config)方法中获取得到
     
    /* 需要传入一个参数ServletContext对象, 获取方法在上面 */
    // 这种方法 获取失败时抛出异常
    ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    // 这种方法 获取失败时返回null
    ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);

    二)、通过加载 配置文件注解配置类 获取

    ① ClassPathXmlApplicationContext:从类路径下的一个或多个xml配置文件中加载上下文定义,适用于xml配置的方式; 这种测试常用

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

    ② FileSystemXmlApplicationContext:从文件系统下的一个或多个xml配置文件中加载上下文定义,也就是说系统盘符中加载xml配置文件;

    ③ XmlWebApplicationContext:从web应用下的一个或多个xml配置文件加载上下文定义,适用于xml配置方式。

    ④AnnotationConfigApplicationContext:从一个或多个基于java的配置类中加载上下文定义,适用于java注解的方式;

    ⑤ AnnotationConfigWebApplicationContext:专门为web应用准备的,适用于注解方式;

     三)、创建一个自己的工具类(SpringContextHolder)实现Spring的ApplicationContextAware接口。

     ① 在配置文件中注册工具类

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

    ② 工具类

    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    /**
     * 实现spring context的管理
     */
    @Component("applicationContextHelper")
    public class ApplicationContextHelper implements ApplicationContextAware {
    
        private static ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext context) throws BeansException {
            applicationContext = context;
        }
      //根据类型获取bean
        public static <T> T popBean(Class<T> clazz){
            checkApplicationContext();
            return applicationContext.getBean(clazz);
        }
       //根据名称获取bean
         public static <T> T popBean(String name){
            checkApplicationContext();
            return applicationContext.getBean(clazz);
        }
       //根据名称和类型获取bean
        public static <T> T popBean(String name, Class<T> clazz){
            checkApplicationContext();
            return applicationContext.getBean(name, clazz);
        }
    
        //检查applicationContext是否为空
        private static void checkApplicationContext(){
            if (applicationContext == null) {
                throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
            }
        }
    }
  • 相关阅读:
    CSUST 8.4 早训
    CSUST 8.5 早训
    hdu1542 Atlantis 线段树--扫描线求面积并
    hdu1540 Tunnel Warfare 线段树/树状数组
    hdu1535 Invitation Cards 最短路
    hdu1358 Period KMP
    SQL Server 向数据库中创建表并添加数据
    初次实践数据库--SQL Server2016
    hdu1301 Jungle Roads 最小生成树
    hdu1281 棋盘游戏 二分图最大匹配
  • 原文地址:https://www.cnblogs.com/FondWang/p/12036918.html
Copyright © 2011-2022 走看看