当 Web 应用集成 Spring 容器后,代表 Spring 容器的WebApplicationContext对象将以

WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 为键存放在ServletContext的属性列表中。您当然可以直接通过以下语句获取 WebApplicationContext:

WebApplicationContext wac = (WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

但通过位于 org.springframework.web.context.support 包中的WebApplicationContextUtils 工具类获取 WebApplicationContext 更方便:

ApplicationContext ac1 =WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 =WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");

说明:
这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。

上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。

servletContext sc 换成

1.servlet.getServletContext()

2.this.getServletContext()

3.request.getSession().getServletContext();

实例:

public class demoServlet extends HttpServlet {
IDemoWS demoWS;
public void init() throws ServletException {
super.init();
ServletContext servletContext = this.getServletContext();
WebApplicationContext ctx =WebApplicationContextUtils.getWebApplicationContext(servletContext);
demoWS = (ISignpicWS)ctx.getBean("demoWS");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
.....//request.getSession().getServletContext()
}
}

  

Spring工具类:WebApplicationContextUtils的更多相关文章

  1. Spring web 工具类 WebApplicationContextUtils

    概述 Spring web 的工具类 WebApplicationContextUtils 位于包 org.springframework.web.context.support 是访问一个Servl ...

  2. spring 工具类大集合

    接以前的文章 apache-commons 常用工具类 和文章 apache-commons 工具类扩展 小家 Spring 对 spring 的工具类做了详细的介绍(一) 这里我抽出一些好用的类,不 ...

  3. Spring工具类 非spring管理环境中获取bean及环境配置

    SpringUtils.java import org.springframework.beans.BeansException; import org.springframework.beans.f ...

  4. JDBC JdbTemplate&NamedParameterJdbcTemplate(Spring工具类)

    使用该工具类需要从spring开发包中导入spring.jar和commons-logging.jar,这个模板是线程安全的.   JdbcTemplate: public class JdbcTem ...

  5. Spring工具类

    文件资源访问 1.统一资源访问接口 Resource 2.实现类 FileSystemResource 通过文件系统路径访问 ClassPathResource 通过classpath路径访问 Ser ...

  6. spring工具类获取bean

    import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebAppl ...

  7. 借助Spring工具类如何实现支持数据嵌套的赋值操作

    假设有两个Bean A和B,想将B中的属性赋值到A实体中,可以使用get set来实现,当属性过多时,就会显得很冗余,可以使用spring提供的BeanUtils.copyProperties()来实 ...

  8. 你可能用到的Spring工具类?

    现在绝大部分项目都已经拥抱Spring生态,掌握Spring常用的工具类,是非常重要,零成本增加编码效率. 一.常用工具类 ObjectUtils org.springframework.util.O ...

  9. Spring工具类ToStringBuilder用法简介

    比如说我们需要打印某个方法的User参数对象 package test; /** * * @author zhengtian * @time 2012-6-28 */ public class Use ...

随机推荐

  1. javascript常量

    javascript中没有常量的概念,虽然许多现代的变成环境可能为您提供了用以创建常量的const语句.对于的自己的变量,可以采用相同的命名约定,并且将他们以静态属性的方式添加到构造函数中. //构造 ...

  2. TYVJ1460 旅行

    描述 A国有n座城市,每座城市都十分美,这使得A国的民众们非常喜欢旅行.然而,A国的交通十分落后,这里只有m条双向的道路,并且这些道路都十分崎岖,有的甚至还是山路,只能靠步行.通过每条道路的长度.泥泞 ...

  3. 推荐两款Xcode插件:KSImageNamed & ColorSense

    之前没怎么接触过Xcode插件,最近发现有人给Xcode做了一些方便编程的插件.今天就推荐两个我个人认为比较好的. 1.KSImageNamed 网站地址 KSImageNamed是一款方便填写图片文 ...

  4. js实现鼠标右键自定义菜单(弹出层),并与树形菜单(TreeView)、iframe合用(兼容IE、Firefox、Chrome)

    <table class="oa-el-panel-tree"> <tr> <td style="vertical-align: top; ...

  5. 并发包之Future:代码级控制超时时间

    先谢Doug Lea. 使用场景: 最近在做webservice调用的时候,发现一个问题,对方的webservice接口很不稳定,所以在获取的数据时候经常要等待很久才能把数据全部拉回来,甚至有时候直接 ...

  6. HDU 2895 编辑距离

    #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> ...

  7. Linux 高可用开源方案 Keepalived VS Heartbeat对比

    1)Keepalived使用更简单:从安装.配置.使用.维护等角度上对比,Keepalived都比Heartbeat要简单得多,尤其是Heartbeat2.1.4后拆分成3个子项目,安装.配置.使用都 ...

  8. Linux: xclip,pbcopy,xsel用法 terminal 复制粘帖 (mac , ubuntu)

    ubuntu下的用户可以只用apt-get来安装: sudo apt-get install xclip echo "Hello, world" | xclip mac下使用pbc ...

  9. load url from future 解释

    利用url 标签之后,不管urlpatterns里的某个地址叫法怎么改变,Templates里的地址都不用修改了.在模版中调用url标签的时候,需要:{% load url from future % ...

  10. table动态添加删除一行和改变标题

    <style type="text/css"> body{ font-size:13px; line-height:25px; } table{ border-top: ...