前言

  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");
}
}
}

获取spring上下文 - applicationContext的更多相关文章

  1. 获取spring的ApplicationContext几种方式【转】

    转自:http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html Java类获取spring 容器的bean 常用的5种获取spring 中bean的方式 ...

  2. JAVA获取Spring上下文

    1. 添加监听 public class SpringContextListener implements ServletContextListener { //获取spring注入的bean对象 p ...

  3. 基于Spring DM管理的Bundle获取Spring上下文对象及指定Bean对象

    在讲述服务注册与引用的随笔中,有提到context.getServiceReferences()方法,通过该方法可以获取到OSGI框架容器中的指定类型的服务引用,从而获取到对应的服务对象.同时该方法还 ...

  4. 怎么获取Spring的ApplicationContext

    在 WEB 开发中,可能会非常少须要显示的获得 ApplicationContext 来得到由 Spring 进行管理的某些 Bean, 今天我就遇到了,在这里和大家分享一下, WEB 开发中,怎么获 ...

  5. quartz的job怎么获取Spring上下文

    第一步.在org.springframework.scheduling.quartz.SchedulerFactoryBean对象中注入applicationContextSchedulerConte ...

  6. 五)Spring + Quartz 复杂业务的两个问题:获取Spring上下文 和 自动注入服务类

    配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  7. 手动获取spring的ApplicationContext和bean对象

    WEB项目: 方法1: 1 ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(S ...

  8. 获取Spring的ApplicationContext的方法

    在网上搜了一下,写一下我试用的两个方法.   1 2 ApplicationContext ctx=new FileSystemXmlApplicationContext("/applica ...

  9. 获取spring上下文的bean 工具类

    有些场景我们不属于controller,service,dao,但是我们需要从spring中得到spring容器里面的bean.这时候我们需要一个类继承 ApplicationContextAware ...

随机推荐

  1. ssm 项目记录用户操作日志和异常日志

    借助网上参考的内容,写出自己记录操作日志的心得!! 我用的是ssm项目使用aop记录日志:这里用到了aop的切点 和 自定义注解方式: 1.建好数据表: 数据库记录的字段有: 日志id .操作人.操作 ...

  2. 以前进行的程序安装创建了挂起的文件操作(SqlServer2000或SqlServer 2000 SP4补丁安装)

    在安装SqlServer 2000或者SqlServer 2000 SP4补丁时常常会出现这样的提示,从而不能进行安装,即使重新启动了计算机,也还是会有同样的提示.在网上查了一下资料,原来是注册表里记 ...

  3. adb-andorid记录当前手机的日志当前显示的app进程及activity,

    adb logcat -v time> /home/sumsang.log adb shell dumpsys window | grep mCurrentFocus

  4. postgresql 增量备份

    介绍: barman是postgresql备份还原的管理工具. 本文环境: 系统: centos6.6 PostgreSQL 9.3.9 barman-1.4.1-1.rhel6.noarch.rpm ...

  5. DELPHI开发LINUX插件架构的程序

    DELPHI开发LINUX插件架构的程序 DELPHI可以开发LINUX配置型插件架构的程序,并且这一套插件架构,同样适用于MSWINDOWS和MAC. 配置插件: 根据配置,动态加载插件:

  6. How to receive JSON as an MVC 5 action method parameter

    How to receive JSON as an MVC 5 action method parameter  解答1 Unfortunately, Dictionary has problems ...

  7. strace命令 一

    简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核 ...

  8. vsnprintf和snprintf(vsnprintf就是为了支持va_list,实现对于sprint功能封装而做的)

    vsnprintf和snprintf是C语言printf家族函数的成员,相关函数列表如下: #include <stdio.h> int printf(const char *format ...

  9. 【转】自动化框架中引入ExtentReport美化报告

    本文链接:https://blog.csdn.net/qq_30353203/article/details/82023922一.先引入三个依赖包 <dependency> <gro ...

  10. MySQL there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause同时创建多个更新当前时间戳字段 解决方法

    问题重现 在写这篇文章之前,明确我的MySQL版本,MariaDB 或者你使用 MySQL 8 也会出现如下问题 MySQL 版本 现在有这样的需求,一张表中有一个字段created_at记录创建该条 ...