获取spring上下文 - applicationContext

获取spring上下文 - applicationContext
前言
  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接口。

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

② 工具类

复制代码
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 popBean(Class clazz){
    checkApplicationContext();
    return applicationContext.getBean(clazz);
    }
       //根据名称获取bean
    public static T popBean(String name){
    checkApplicationContext();
    return applicationContext.getBean(clazz);
    }
       //根据名称和类型获取bean
    public static T popBean(String name, Class clazz){
    checkApplicationContext();
    return applicationContext.getBean(name, clazz);
    }

    //检查applicationContext是否为空
    private static void checkApplicationContext(){
    if (applicationContext == null) {
    throw new IllegalStateException(“applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder”);
    }
    }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值