`
liuhello
  • 浏览: 69235 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

spring webapplicationcontext

阅读更多

ApplicationContext是Spring的核心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些,ApplicationContext则是“应用的容器”了:P,Spring把Bean放在这个容器中,在需要的时候,用getBean方法取出,虽然我没有看过这一部分的源代码,但我想它应该是一个类似Map的结构。 
在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> 

<!-- OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER 
<servlet> 
<servlet-name>context</servlet-name> 
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> 
<load-on-startup>1</load-on-startup> 
</servlet> 
--> 

可以看出,有两种方法,一个是用ContextLoaderListener这个Listerner,另一个是ContextLoaderServlet这个Servlet,这两个方法都是在web应用启动的时候来初始化WebApplicationContext,我个人认为Listerner要比Servlet更好一些,因为Listerner监听应用的启动和结束,而Servlet得启动要稍微延迟一些,如果在这时要做一些业务的操作,启动的前后顺序是有影响的。 

那么在ContextLoaderListener和ContextLoaderServlet中到底做了什么呢? 
以ContextLoaderListener为例,我们可以看到 
public void contextInitialized(ServletContextEvent event) { 
this.contextLoader = createContextLoader(); 
this.contextLoader.initWebApplicationContext(event.getServletContext()); 

protected ContextLoader createContextLoader() { 
return new ContextLoader(); 

ContextLoader是一个工具类,用来初始化WebApplicationContext,其主要方法就是initWebApplicationContext,我们继续追踪initWebApplicationContext这个方法(具体代码我不贴出,大家可以看Spring中的源码),我们发现,原来ContextLoader是把WebApplicationContext(XmlWebApplicationContext是默认实现类)放在了ServletContext中,ServletContext也是一个“容器”,也是一个类似Map的结构,而WebApplicationContext在ServletContext中的KEY就是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,我们如果要使用WebApplicationContext则需要从ServletContext取出,Spring提供了一个WebApplicationContextUtils类,可以方便的取出WebApplicationContext,只要把ServletContext传入就可以了。 

上面我们介绍了WebApplicationContext在Servlet容器中初始化的原理,一般的Web应用就可以轻松的使用了,但是,随着Struts的广泛应用,把Struts和Spring整个起来,是一个需要面对的问题,Spring本身也提供了Struts的相关类,主要使用的有org.springframework.web.struts.ActionSupport,我们只要把自己的Action继承自ActionSupport,就是可以调用ActionSupport中getWebApplicationContext()的方法取出WebApplicationContext,但这样一来在Action中,需要取得业务逻辑的地方都要getBean,看上去不够简洁,所以Spring又提供了另一个方法,用org.springframework.web.struts.ContextLoaderPlugIn,这是一个Struts的Plug,在Struts启动时加载,对于Action,可以像管理Bean一样来管理,在struts-config.xml中Action的配置变成类似下面的样子 
<action attribute="aForm" name="aForm" path="/aAction" scope="request" type="org.springframework.web.struts.DelegatingActionProxy"> 
<forward name="forward" path="forward.jsp" /> 
</action> 
注意type变成了org.springframework.web.struts.DelegatingActionProxy,之后我们需要建立action-servlet.xml这样的文件,action-servlet.xml符合Spring的spring-beans.dtd标准,在里面定义类似下面的 
<bean name="/aAction" class="com.web.action.Aaction" singleton="false"> 
<property name="businessService"> 
<ref bean="businessService"/> 
</property> 
</bean> 

com.web.action.Aaction是Action的实现类,businessService是需要的业务逻辑,Spring会把businessService注入到Action中,在Action中只要写businessService的get和set方法就可以了,还有一点,action的bean是singleton="false",即每次新建一个实例,这也解决了Struts中Action的线程同步问题,具体过程是当用户做“/aAction”的HTTP请求(当然应该是“/aAction.do”),Struts会找到这个Action的对应类org.springframework.web.struts.DelegatingActionProxy,DelegatingActionProxy是个代理类,它会去找action-servlet.xml文件中“/aAction”对应的真正实现类,然后把它实例化,同时把需要的业务对象注入,然后执行Action的execute方法。 

使用了ContextLoaderPlugIn,在struts-config.xml中变成类似这样配置 
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> 
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml" /> 
</plug-in> 
而在web.xml中不再需要ContextLoaderListener或是ContextLoaderServlet。 

说到这里不知道大家会不会有这样的问题,如果使用ContextLoaderPlugIn,如果我们有些程序是脱离Struts的Action环境,我们怎么处理,比如我们要自定义标记库,在标记库中,我们需要调用Spring管理的业务层逻辑对象,这时候我们就很麻烦,因为只有在action中动态注入业务逻辑,其他我们似乎不能取得Spring的WebApplicationContext。 

别急,我们还是来看一下ContextLoaderPlugIn的源码(源码不再贴出),我们可以发现,原来ContextLoaderPlugIn仍然是把WebApplicationContext放在ServletContext中,只是这个KEY不太一样了,这个KEY值为ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX+ModuleConfig.getPrefix()(具体请查看源代码),这下好了,我们知道了WebApplicationContext放在哪里,只要我们在Web应用中能够取到ServletContext也就能取到WebApplicationContext了:) 

Spring是一个很强大的框架,希望大家在使用过程中不断的深入,了解其更多的特性,我在这里抛砖引玉,有什么不对的地方,请大家指出

分享到:
评论

相关推荐

    Spring MVC开发配置文件 applicationContext

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

    Spring2.5中文手册

    - Spring WebApplicationContext扩展了普通的ApplicationContext,为Web应用提供了特定的功能,如处理请求参数、session和ServletContext访问等。 这个`Spring2.5中文手册`包含了上述所有概念的详细解释和示例,...

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

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

    吴老师教学讲义-一步一步用SpringSecurity3

    5. 配置web.xml:在web.xml文件中添加一个过滤器,这个过滤器是DelegatingFilterProxy,它代理了SpringBean中定义的过滤器,从而让bean能够在Spring WebApplicationContext生命周期内正常工作。 6. 启动Spring容器...

    spring与flex(blazeds)注解配置

    `ContextLoaderListener`是Spring的一部分,负责初始化Spring WebApplicationContext,加载配置并启动应用上下文。 最后,`DispatcherServlet`(名为flex)是Spring MVC的关键组件,它负责处理HTTP请求并分发到合适...

    springmvc+spring

    6. **Web应用上下文**:Spring WebApplicationContext为Web应用提供了特定的上下文,能够加载与Web相关的bean,并且方便地与其他Web组件(如Servlet、Filter)进行交互。 7. **Spring Boot**:Spring Boot是基于...

    spring框架中常用的配置

    - **ContextLoaderListener**:这是一个重要的监听器,它会在Web应用程序启动时初始化Spring的WebApplicationContext。通常情况下,开发者会在`web.xml`中配置此监听器以及应用程序上下文的位置。 - **配置示例**...

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

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

    Java常见异常集-Java,Hibernate,Tomcat异常

    11. **ServletException: IllegalStateException: No WebApplicationContext found**:此异常表明Spring WebApplicationContext未被正确注册,通常发生在没有添加ContextLoaderListener到web.xml中。确保在配置文件...

    spring 资料spring 资料spring 资料spring 资料

    2. **容器**:Spring框架提供了两种类型的容器:Spring Bean容器(也称为ApplicationContext)和Web应用上下文(WebApplicationContext)。容器负责创建、配置和管理对象(称为Bean),并处理对象间的依赖关系。 3....

    SpringTest_springtest_spring_java_Framework_

    Spring Boot Test则为Spring Boot应用提供了更便捷的测试工具,包括对MockMVC、WebApplicationContext和嵌入式Servlet容器的支持。 "spring"和"java Framework"明确了讨论的焦点是基于Java的Spring框架。Spring框架...

    Spring Web MVC外文翻译

    ### Spring Web MVC 外文翻译知识点解析 #### 一、Spring Web MVC介绍 ...特别是 DispatcherServlet 的配置及其与 WebApplicationContext 的交互方式,是深入掌握 Spring Web MVC 不可缺少的一部分。

    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 jar 包详解

    - **功能简介**:包含了 Web 应用开发时使用 Spring 框架时所需的核心类,包括自动载入 WebApplicationContext 特性的类、Struts 与 JSF 集成类、文件上传的支持类、Filter 类和大量工具辅助类。 - **应用场景**:...

    Spring整合Struts

    ContextLoaderPlugin是Spring为Struts设计的一个插件,它能够加载一个Spring配置文件,该文件指向由ContextLoaderListener加载的根WebApplicationContext作为其父上下文。默认情况下,配置文件的名称是映射的servlet...

    spring2.0和spring2.5 及以上版本的jar包区别 spring jar 包详解

    11. **spring-web.jar**:为基于Web的应用提供了支持,包括对WebApplicationContext的支持、各种视图技术(如JSP、Freemarker等)的集成。 12. **spring-webmvc.jar**:提供了模型-视图-控制器(Model-View-...

    Spring 管理filter 和servlet

    在`web.xml`中,使用`ContextLoaderListener`来初始化Spring的WebApplicationContext,确保在Filter执行之前Spring的上下文已经被加载。配置如下: ```xml &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;/...

    Spring4JAR包、Spring4API,Spring4源码

    `WebApplicationContext`接口新增了获取Servlet配置信息的方法;`MessageSource`接口也得到了改进,支持更灵活的消息解析。 Spring4还增强了数据访问层的支持。对JDBC、Hibernate、JPA等持久化技术的集成更加紧密,...

    spring mvc jar包

    4. `org.springframework.web-3.0.2.RELEASE.jar`:这个库包含了一些 Web 相关的支持类,如 Web 应用上下文(WebApplicationContext)、HTTP 拦截器(HandlerInterceptor)和请求映射(RequestMapping)等,它们与 ...

Global site tag (gtag.js) - Google Analytics