Servlet 获取 ApplicationContext
一般使用Spring完成了注入,在Service或SpringMVC 中可以通过注解的形式来获取 Spring的已经注入的Spring的bean如下所示:
@Resource(name = "userInfoMapper")
private UserInfoMapper userInfoMapper;
但是有些情况如在servlet中该如何获取该对象呢,因为SpringMVC 是基于Servlet的,所有的请求先通过一个默认的Servlet处理——org.springframework.web.servlet.DispatcherServlet(此选项在Web.xml中配置SpringMVC时,必须配置),所以手动建立的Servlet是不能自动获取注入的Bean的。需要通过ApplicationContext applicationContext 来获取:
this.xmlPacker = ((IXmlPacker) this.applicationContext.getBean("xmlPacker"));
applicationContext 的获取方法是添加一个监听器,在Context完成时进行初始化,具体的实现方法是:
继承ServletContextListener,重写contextInitialized,给webCtx赋值。
package com.casic.servlet;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ApplicationContextInit implements ServletContextListener {
private static ApplicationContext webCtx = null;
public void contextDestroyed(ServletContextEvent event) {
}
public void contextInitialized(ServletContextEvent event) {
webCtx = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
}
public static ApplicationContext getWebApplicationContext() {
return webCtx;
}
public static void setWebCtx(ApplicationContext webCtx) {
webCtx = webCtx;
}
}
在web.xml 中添加监听器
<listener>
<listener-class>com.casic.servlet.ApplicationContextInit</listener-class>
</listener>
servlet 在 init()函数中使用ApplicationContextInit.getWebApplicationContext() 获取
public void init() throws ServletException {
if (this.applicationContext == null) {
this.applicationContext = ApplicationContextInit.getWebApplicationContext();
this.xmlPacker = ((IXmlPacker) this.applicationContext.getBean("xmlPacker"));
}
}
Servlet 获取 ApplicationContext的更多相关文章
- Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式
转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236 Spring获取ApplicationContex ...
- springMVC 使用WebApplicationContext获取ApplicationContext对象
主要用于从application中获取bean 1.applicationContext 在web.xml中使用listener配置 <context-param> <param-n ...
- spring中获取applicationContext(2)
前几天写web项目的时候,用到了spring mvc. 但是又写bean.我要在代码里面生成,而这个bean里面,又有一些属性是通过spring注入的. 所以,只能通过ApplicationConte ...
- spring中获取applicationContext
常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXm ...
- 获取applicationContext对象的方法
方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext(&quo ...
- 在web项目中获取ApplicationContext上下文的3种主要方式及适用情况
最近在做web项目,需要写一些工具方法,涉及到通过Java代码来获取spring中配置的bean,并对该bean进行操作的情形.而最关键的一步就是获取ApplicationContext,过程中纠结和 ...
- javaWeb普通类获取ApplicationContext
网上看了很多关于获取ApplicationContext的方法,五大方法,但是我用web服务使用成功的就这一个,自己记忆下. import javax.servlet.ServletContextEv ...
- Spring获取ApplicationContext
在Spring+Struts+Hibernate中,有时需要使用到Spring上下文.项目启动时,会自动根据applicationContext配置文件初始化上下文,可以使用ApplicationCo ...
- servlet获取参数时,request.getParameter("id")参数获取失败
servlet获取参数时,request.getParameter("id")参数获取失败,这里的参数是“index”里面href中的参数 要注意,取不到值,是不是要取的参数有没有 ...
随机推荐
- WAMP2.5 Forbidden
Forbidden You don't have permission to access /DuoLamPHP/index.php on this server. Apache/2.4.9 (Win ...
- 解药还是毒药(codevs 2594)
2594 解药还是毒药 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description Smart研制出对 ...
- 在MVC的项目中访问静态页面
MVC在生成项目的时候会生成的WEB-INF底下.这个文件夹下面的文件是受保护的,都会走MVC的流程, 但是我希望在WebContent底下可以使用静态页面, 那么需要进入springmvc-serv ...
- jieba
# coding: utf-8 # ###jieba特性介绍 # 支持三种分词模式: # 精确模式,试图将句子最精确地切开,适合文本分析: # 全模式,把句子中所有的可以成词的词语都扫描出来, 速度非 ...
- mysql里表以及列的增删改查
在一个表里插入数据(增) insert into 表名 (需要插入的列名如 id,name,age)values (1,'张三',20), (2,'李四',30): 查询表内容(查 ...
- Nginx详解(一)
1.Nginx是什么? Nginx就是反向代理服务器. 首先我们先来看看什么是代理服务器,代理服务器一般是指局域网内部的机器通过代理服务发送请求到互联网上的服务器,代理服务器一般作用于客户端.比如Go ...
- 【翻译十四】java-并发之保护块儿
Guarded Blocks Threads often have to coordinate their actions. The most common coordination idiom is ...
- 无废话ExtJs 入门教程十六[页面布局:Layout]
无废话ExtJs 入门教程十六[页面布局:Layout] extjs技术交流,欢迎加群(201926085) 首先解释什么是布局: 来自百度词典的官方解释:◎ 布局 bùjú: [distributi ...
- 2014百度之星资格赛 1004:Labyrinth(DP)
Labyrinth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- MyEclipse 关闭鼠标悬停提示
preference --> MyEclipse -->Files and Editors--> Common Editor Preference --> Hovers 把里面 ...