`
arpenker
  • 浏览: 343471 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

WebApplicationContext

阅读更多
淘二哥女装专业导购
http://www.tao2ge.com

如果您想要在自己所定义的Servlet类别中使用Spring的容器功能,则也可以使用 org.springframework.web.context.ContextLoaderListener,例如在web.xml中使用< listener>标签加以定义:

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


ContextLoaderListener预设会读取applicationContext.xml,您可以指定自己的定义档,只要在<context-param>中指定"contextConfigLocation"参数,例如:

...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans-config.xml,
→ /WEB-INF/demo-service.xml</param-value>
</context-param>
...


接着您可以在自定义的Servlet中使用 org.springframework.web.context.support.WebApplicationContextUtils,从 ServletContext中取得org.springframework.web.context.WebApplicationContext,例如:

WebApplicationContext ctx =
WebApplicationContextUtils.
getRequiredWebApplicationContext(
this.getServletContext());


WebApplicationContext实作了ApplicationContext介面,是Spring专为Servlet的Web应用程式设计的 ApplicationContext实作类别,在取得WebApplicationContext之后,您可以利用它来取得Bean定义档中定义的 Bean实例,例如:
Date date = (Date) ctx.getBean("dateBean");


在不支援Listener设定的容器上(例如Servlet 2.2以更早的版本),您可以使用org.springframework.web.context.ContextLoaderServlet来取代上面的ContextLoaderListener的设定,例如:

...
<servlet>
<servlet-name>contextLoader</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
...
分享到:
评论

相关推荐

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

    Spring 获取 WebApplicationContext、ApplicationContext 几种方法详解 在 Spring 框架中,获取 WebApplicationContext 和 ApplicationContext 对象是非常重要的,因为它们提供了访问 Spring 容器中的 Bean 对象的...

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

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

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

    Spring MVC是Java领域非常流行的Web框架,而WebApplicationContext是Spring MVC中用于Web应用的一种特殊的ApplicationContext。在了解WebApplicationContext之前,我们先简单回顾一下Spring的IoC容器。IoC...

    SpringMVC中的RootApplicationContext上下文和WebApplicationContext上下文,通过注解配置SpringMVC的完整解决方案

    注解配置SpringMVC原理简述1. 准备知识1.1 两个应用上下文1.2 ServletContext配置方法(Configuration Methods)1.3 运行时插拔1.4 SpringServletContainerInitializer1.4.1 AbstractContextLoaderInitializer1.4.2 ...

    Struts+Spring+Hibernate 简单例子开发

    public void setWebApplicationContext(WebApplicationContext webApplicationContext) { this.webApplicationContext = webApplicationContext; } // 其他方法和属性 } ``` - **视图层**:使用两个JSP页面...

    Spring如何获取Bean

    WebApplicationContext webApplicationContext = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); MyBean myBean = (MyBean) webApplicationContext....

    Spring框架系列(13) - SpringMVC实现原理之DispatcherServlet的初始化过程.doc

    大多数应用程序只有一个 WebApplicationContext,除此之外也可以一个 Root WebApplicationContext 被多个 Servlet 实例,然后各自拥有自己的 Servlet WebApplicationContext 配置。Root WebApplicationContext 包含...

    Spring在容器在启动的时候发生了什么

    `ContextLoader`的核心方法是`initWebApplicationContext`,它负责根据`contextConfigLocation`参数指定的XML配置文件路径,初始化`WebApplicationContext`。这些配置文件通常包含了Spring的bean定义,如服务、数据...

    Spring Web MVC外文翻译

    `WebApplicationContext` 与 `ServletContext` 和 Servlet 相关联,并且与 `ServletContext` 绑定,这样应用程序就可以通过静态方法 `RequestContextUtils` 来查找 `WebApplicationContext`,如果它们需要访问它的话...

    spring在web.xml中和在struts中的不同配置.[收集].pdf

    在Web应用中,我们使用的是WebApplicationContext,它是ApplicationContext的子类,专门针对Web环境设计。在初始化WebApplicationContext时,我们通常会在`web.xml`中设置相关的配置参数。 在`web.xml`中,有两种...

    spring3.x的读书笔记3

    在Spring框架中,WebApplicationContext是专门为Web应用设计的ApplicationContext,它与普通的ApplicationContext相比,增加了对Web环境的支持,比如能够访问Servlet上下文(ServletContext)以及处理HTTP请求等。...

    44 Spring控制器Controller如何设置AOP?慕课专栏1

    `ContextLoaderListener`创建的ApplicationContext存储在`ServletContext`的一个属性中,键为`"org.springframework.web.context.WebApplicationContext.ROOT"`,而`DispatcherServlet`创建的WebApplicationContext...

    Spring源代码解析(二):IoC容器在Web容器中的启动.doc

    在Web MVC场景下,除了根`WebApplicationContext`外,还会创建一个或多个子`WebApplicationContext`,通常用于处理特定的Servlet或DispatcherServlet。这些子上下文与根上下文形成层次结构,使得我们可以将业务逻辑...

    框架源码专题

    WebApplicationContext context = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (context == null) { context = ...

    精品专题(2021-2022年收藏)spring项目中监听器作用.doc

    1. **初始化WebApplicationContext**:当`contextInitialized()`方法被调用时,`ContextLoaderListener`会创建一个新的`ContextLoader`实例,并通过`initWebApplicationContext()`方法初始化WebApplicationContext。...

    几种spring获取bean的方法.txt

    可以通过`WebApplicationContextUtils`工具类中的静态方法`getWebApplicationContext()`来获取当前Web应用的`ApplicationContext`实例。一旦获取到了`ApplicationContext`,就可以调用其`getBean()`方法来获取特定...

    深度解析springMvc

    - 在创建 `WebApplicationContext` 的过程中,会调用 `configureAndRefreshWebApplicationContext()` 方法来配置并刷新应用上下文,这是 Spring IOC 容器启动的关键步骤。 - 最终,通过调用 `onRefresh()` 方法来...

    spring的mvc.doc

    `initServletBean()`进一步调用了`initWebApplicationContext()`,将Servlet的`ServletContext`和`ServletConfig`信息传递给`WebApplicationContext`。 2. `ServletConfig`与`ServletContext`: - `ServletConfig`...

    spring源代码解析

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

    spring中的所有配置

    当Web容器启动时,它会触发`ContextLoaderListener`,该监听器将读取并创建一个WebApplicationContext。这通常在`web.xml`文件中通过`&lt;listener&gt;`标签进行配置: ```xml &lt;listener-class&gt;org.springframework.web...

Global site tag (gtag.js) - Google Analytics