`
kidfang
  • 浏览: 15370 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

Spring中的WebApplicationContext

阅读更多

参考

http://hi.baidu.com/yawiki/blog/item/bff69822c1556bf3d6cae264.html

文中指出

在Web应用中,我们会用到WebApplicationContext,WebApplicationContext继承自ApplicationContext,先让我们看看在Web应用中,怎么初始化WebApplicationContext,在web.xml中定义:

<context-param>

    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

实际配置如下,用的就是ContextLoaderListener

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <display-name>XMNMS</display-name>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/log4j.properties</param-value>
  </context-param>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/applicationContext-basic.xml
      /WEB-INF/applicationContext-security-acegi.xml
    </param-value>
  </context-param>

   <filter-mapping>
    <filter-name>Acegi Filter Chain Proxy</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--
  - Loads the root application context of this web app at startup.
  - The application context is then available via
  - WebApplicationContextUtils.getWebApplicationContext(servletContext).
  -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>

  <!--
         The HttpSessionEventPublisher will publish
         HttpSessionCreatedEvent and HttpSessionDestroyedEvent
         to the WebApplicationContext
  -->
  <listener>
    <listener-class>net.sf.acegisecurity.ui.session.HttpSessionEventPublisher</listener-class>
  </listener>

</web-app>

 

 

页面中调用,如下
<%@ page import="org.springframework.web.context.WebApplicationContext"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@ page import="sample.service.IContactManager"%>

<%
  WebApplicationContext wac =WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext());
  IContactManager contactManager= (IContactManager) wac.getBean("contactManager");
  List contacts=null;
  try{
    //有授权
    contacts=contactManager.getAll();
  } catch(Exception e){
    e.printStackTrace();
  }

%>

分享到:
评论

相关推荐

    Spring获取webapplicationcontext,applicationcontext几种方法详解

    Spring获取webapplicationcontext,applicationcontext几种方法详解

    在web容器(WebApplicationContext)中获取spring中的bean

    Spring把Bean放在这个容器中,普通的类在需要的时候,直接用getBean()方法取出

    spring jar 包详解

    (11) spring-web.jar 这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、 Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 (12) ...

    Spring MVC开发配置文件 applicationContext

    Spring Web MVC开发 xml配置文件格式,无bean之类 Spring Web MVC开发配置文件 applicationContext

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring MVC之WebApplicationContext_动力节点Java学院整理

    主要介绍了Spring MVC之WebApplicationContext的相关资料,需要的朋友可以参考下

    spring-web-2.5.jar

    org.springframework.web.context.WebApplicationContext.class org.springframework.web.context.request.AbstractRequestAttributes.class org.springframework.web.context.request....

    spring-web-5.3.6 jar包.rar

    这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类, 包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 spring的核心类,提供了核心HTTP...

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

     这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 (12) spring-webmvc.jar  这个...

    spring4.1核心包

    包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 18. spring-webmvc-4.1.1.RELEASE.jar 包含...

    Spring的注入在Servlet中使用

    Spring的注入在Servlet中使用:在Servlet中使用Spring注入的信息,需要WebApplicationContext这个专门为Web准备的应用上下文

    Spring面试题含答案.pdf

    35. 你可以在 Spring 中注入一个 null 和一个空字符串吗? 36. 什么是基于 Java 的 Spring 注解配置? 给一些注解的例子 37. 什么是基于注解的容器配置? 38. 怎样开启注解装配? 39. @Required 注解 40. @Autowired ...

    最新最全的spring开发包

     这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 (12) spring-webmvc.jar 这个...

    spring源代码解析

    对于一个Spring激活的web应用程序,可以通过使用Spring代码声明式的指定在web应用程序启动时载入应用程序上下文(WebApplicationContext),Spring的ContextLoader是提供这样性能的类,我们可以使用 ...

    Struts2+Spring3+MyBatis3完整实例

    - Initializing Spring root WebApplicationContext - Root WebApplicationContext: initialization started - Refreshing Root WebApplicationContext: startup date [Tue Jun 14 11:08:28 CST 2011]; root of ...

    spring+springmvc+mybatis的整合

    这里就要导入mybatis和spring的整合包了,这里sqlsession中也要导入mybatis的配置文件 2.6 spring-bean 配置mapper自动扫描 MapperScannerConfigurer将扫描basePackage所指定的包下的所有接口类(包括子类), ...

    开源框架 Spring Gossip

    第一个 Spring MVC 程式 WebApplicationContext Handler Mapping Handler Interceptor Controller 继承架构 ModelAndView View Resolver Exception Resolver 使用 Controller ...

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring+hibernate+osworkflow

    spring+hibernate+osworkflow,服务器为Tomcat6.0,数据库为Mysql 把viewlivegraph2.jsp改为viewlivegraph.jsp. 其中把wf的获得修改为 ApplicationContext cxt = WebApplicationContextUtils.get...

Global site tag (gtag.js) - Google Analytics