获取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接口。
① 在配置文件中注册工具类
<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的更多相关文章
- 获取spring的ApplicationContext几种方式【转】
转自:http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html Java类获取spring 容器的bean 常用的5种获取spring 中bean的方式 ...
- JAVA获取Spring上下文
1. 添加监听 public class SpringContextListener implements ServletContextListener { //获取spring注入的bean对象 p ...
- 基于Spring DM管理的Bundle获取Spring上下文对象及指定Bean对象
在讲述服务注册与引用的随笔中,有提到context.getServiceReferences()方法,通过该方法可以获取到OSGI框架容器中的指定类型的服务引用,从而获取到对应的服务对象.同时该方法还 ...
- 怎么获取Spring的ApplicationContext
在 WEB 开发中,可能会非常少须要显示的获得 ApplicationContext 来得到由 Spring 进行管理的某些 Bean, 今天我就遇到了,在这里和大家分享一下, WEB 开发中,怎么获 ...
- quartz的job怎么获取Spring上下文
第一步.在org.springframework.scheduling.quartz.SchedulerFactoryBean对象中注入applicationContextSchedulerConte ...
- 五)Spring + Quartz 复杂业务的两个问题:获取Spring上下文 和 自动注入服务类
配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...
- 手动获取spring的ApplicationContext和bean对象
WEB项目: 方法1: 1 ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(S ...
- 获取Spring的ApplicationContext的方法
在网上搜了一下,写一下我试用的两个方法. 1 2 ApplicationContext ctx=new FileSystemXmlApplicationContext("/applica ...
- 获取spring上下文的bean 工具类
有些场景我们不属于controller,service,dao,但是我们需要从spring中得到spring容器里面的bean.这时候我们需要一个类继承 ApplicationContextAware ...
随机推荐
- js jquery 实现 排班,轮班,日历,日程。使用fullcalendar 插件
如果想用fullcalendar实现排班功能,或者日历.日程功能.那么只需要简单的几步: 这里先挂官网链接: fullcalendar fullcalendar官网下载链接 一.下载及简单配置 1.这 ...
- 关于Vertical Align的理解
1:vertical-align 翻译就是垂直-对齐... 2:关于line-height的点 2.1:如果一个标签没有定义height属性,那么其最终表现的高度一定是由line-height起作用. ...
- js检测手机上是否有此APP,有的话打开应用,没有的话跳转到appstore
//html代码中 的 a 标签,以微信为例,默认的是调用weixin scheme,去打开本机的微信,如果没有则跳转到相应连接 <a href="weixin://" cl ...
- Spring boot Security 登陆安全配置
实现的效果 访问url时,如果未登录时跳转到Login界面,要求用户登陆,如果登陆过返回请求的数据. 效果图 访问数据时,未登录返回login界面 登陆操作 登陆成功进入登出界面 登陆成功后再次访问数 ...
- mac安装rust
1.安装 curl https://sh.rustup.rs -sSf | sh 使用brew各种出错,还慢. 2编译 source $HOME/.cargo/env 3.版本查看 rustc --v ...
- html中a标签的常见用法
html中a标签的常见用法 一.总结 一句话总结: a.页面跳转 b.使用锚点定位 c.下载文件 二.html中<a>标签的用法 转自或参考:html中<a>标签的用法http ...
- receipt
receipt - 必应词典 美[riˈsiːt]英[rɪ'siːt] n.收据:收入:接受:字据 v.开收据 网络收到:收条:发票 变形复数:receipts: 搭配give receipt:sig ...
- 似然函数 | 最大似然估计 | likelihood | maximum likelihood estimation | R代码
学贝叶斯方法时绕不过去的一个问题,现在系统地总结一下. 之前过于纠结字眼,似然和概率到底有什么区别?以及这一个奇妙的对等关系(其实连续才是f,离散就是p). 似然函数 | 似然值 wiki:在数理统计 ...
- pip安装报错Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-e_k8hq6a/pynacl/
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-e_k8hq6a/pyn ...
- redis-cli 使用密码登录
#./redis-cli 输入auth +空格+ 刚才设置的密码 成功