一般使用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的更多相关文章

  1. Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式

    转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236  Spring获取ApplicationContex ...

  2. springMVC 使用WebApplicationContext获取ApplicationContext对象

    主要用于从application中获取bean 1.applicationContext 在web.xml中使用listener配置 <context-param> <param-n ...

  3. spring中获取applicationContext(2)

    前几天写web项目的时候,用到了spring mvc. 但是又写bean.我要在代码里面生成,而这个bean里面,又有一些属性是通过spring注入的. 所以,只能通过ApplicationConte ...

  4. spring中获取applicationContext

    常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXm ...

  5. 获取applicationContext对象的方法

    方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext(&quo ...

  6. 在web项目中获取ApplicationContext上下文的3种主要方式及适用情况

    最近在做web项目,需要写一些工具方法,涉及到通过Java代码来获取spring中配置的bean,并对该bean进行操作的情形.而最关键的一步就是获取ApplicationContext,过程中纠结和 ...

  7. javaWeb普通类获取ApplicationContext

    网上看了很多关于获取ApplicationContext的方法,五大方法,但是我用web服务使用成功的就这一个,自己记忆下. import javax.servlet.ServletContextEv ...

  8. Spring获取ApplicationContext

    在Spring+Struts+Hibernate中,有时需要使用到Spring上下文.项目启动时,会自动根据applicationContext配置文件初始化上下文,可以使用ApplicationCo ...

  9. servlet获取参数时,request.getParameter("id")参数获取失败

    servlet获取参数时,request.getParameter("id")参数获取失败,这里的参数是“index”里面href中的参数 要注意,取不到值,是不是要取的参数有没有 ...

随机推荐

  1. 模拟赛1102d2

    /* φ(n)=φ(p^k)=p^k-p^(k-1)=(p-1)*p^(k-1) φ(m*n)=φ(m)*φ(n) 直接套公式做,因为分解质因数时,只分解一个数,所以可以不打素数表,只将n分解到√n就 ...

  2. Sublime Text 无法使用Package Control的解决方法 以及 常用的插件安装过程

    大概一个月之前给 Macbook air 装 Sublime Text 3 的时候,遇到过这个问题,当时解决了,现在回想,感觉忘的七七八八了,赶紧趁着还没有全忘光的时候记下来,当时的过程记得不一定准确 ...

  3. HDU3359 Kind of a Blur(高斯消元)

    建立方程后消元 #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring& ...

  4. VS2013 当前不会命中断点,还没有为该文档加载任何符号

    方法一: 把ie的 调试 打开,然后调试的时候 会问你 是在新示例中打开 还是 当前示例,你选择当前的就行了.还有 建议你用 ie8.0的 开发者工具 调试  非常舒服 我已经 早就不用debuger ...

  5. 跨平台C的IDE

    1.JetBrains的新跨平台C++ IDE,CLion已经开始EAP了,不过这货是收费的 http://confluence.jetbrains.com/display/CLION/Early+A ...

  6. Win10 保存Element到相册

    保存控件到相册 private async void saveImg_Click(object sender, RoutedEventArgs e) { StorageFolder savedPics ...

  7. WPF QuickStart系列之样式和模板(Style and Template)

    在WPF桌面程序中,当我们想构建一个统一的UI表现时(在不同操作系统下,显示效果一致),此时我们就需要使用到WPF中的样式和模板技术.简单来说,如果我们需要简单的给一个Button设置宽,高,Marg ...

  8. Windows Live Writer技巧

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:今天的内容虽然和开发技术无关,却应该和喜欢写东西的技术人员有关,比如我所有的文章都是用 ...

  9. JavaScript前端框架的思考

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:现在JavaScript前端框架层出不穷,尤其Angular进入到2.x时候之后,我们 ...

  10. Effective C++ 之 Item 4:确定对象被使用前已先被初始化

    Effective C++ Chapter 1. 让自己习惯C++ (Accustoming Yourself to C++) Item 4. 确定对象被使用前已先被初始化 (Make sure th ...