`

spring有三种启动方式,使用ContextLoaderServlet,ContextLoader

阅读更多

spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn.
看一下ContextLoaderListener的源码,这是一个ServletContextListener
/**
   * Initialize the root web application context.
   */
public void contextInitialized(ServletContextEvent event) {
   this.contextLoader = createContextLoader();
   this.contextLoader.initWebApplicationContext(event.getServletContext());
}

   /**
   * Create the ContextLoader to use. Can be overridden in subclasses.
   * @return the new ContextLoader
   */
protected ContextLoader createContextLoader() {
   return new ContextLoader();
}

contextLoader的源码
public WebApplicationContext initWebApplicationContext(ServletContext servletContext)
    throws BeansException {

   long startTime = System.currentTimeMillis();
   if (logger.isInfoEnabled()) {
    logger.info("Root WebApplicationContext: initialization started");
   }
   servletContext.log("Loading Spring root WebApplicationContext");

   try {
    // Determine parent for root web application context, if any.
    ApplicationContext parent = loadParentContext(servletContext);

    WebApplicationContext wac = createWebApplicationContext(servletContext, parent);
    servletContext.setAttribute(
      WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

    if (logger.isInfoEnabled()) {
     logger.info("Using context class [" + wac.getClass().getName() +
       "] for root WebApplicationContext");
    }
    if (logger.isDebugEnabled()) {
     logger.debug("Published root WebApplicationContext [" + wac +
       "] as ServletContext attribute with name [" +
       WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
    }

    if (logger.isInfoEnabled()) {
     long elapsedTime = System.currentTimeMillis() - startTime;
     logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
    }

    return wac;
   }
   catch (RuntimeException ex) {
    logger.error("Context initialization failed", ex);
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
    throw ex;
   }
   catch (Error err) {
    logger.error("Context initialization failed", err);
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
    throw err;
   }
}
注意WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,这里面放了WebApplicationContext,需要使用时从ServletContext取出
可以使用WebApplicationContextUtils得到WebApplicationContext
public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
   Object attr = sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
   if (attr == null) {
    return null;
   }
   if (attr instanceof RuntimeException) {
    throw (RuntimeException) attr;
   }
   if (attr instanceof Error) {
    throw (Error) attr;
   }
   if (!(attr instanceof WebApplicationContext)) {
    throw new IllegalStateException("Root context attribute is not of type WebApplicationContext: " + attr);
   }
   return (WebApplicationContext) attr;
}
关键的问题在于struts如何启动的spring的,ContextLoaderPlugIn的源码

// Publish the context as a servlet context attribute.
   String attrName = getServletContextAttributeName();
   getServletContext().setAttribute(attrName, wac);

public String getServletContextAttributeName() {
   return SERVLET_CONTEXT_PREFIX + getModulePrefix();
}
不同加载的Key竟然不同,原因就是WebApplicationContext放在那里的问题,可spring调用的时候会根据WebApplicationContext里面定义的那个名字去找的,问题出在这里


在struts-config.xml中配置
     <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
       <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
     </plug-in>

     <controller>
         <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" />
     </controller>


原理是这样的,Struts虽然只能有一个ActionServlet实例,但是对于不同的子应用分别能有自己的RequestProcessor实例每个RequestProcessor实例分别对应不同的struts配置文件。
    子应用的ProcessorClass类必须重写一般就是继承RequestProcessor类,然后再其配置文件的controller元素中的<processorClass>属性中作出修改。那么当
   getRequestProcessor(getModuleConfig(request)).process(request,response); 就能根据request选择相应的moduleconfig,再根据其<processorClass>属性选择相应的 RequestProcessor子类来处理相应的请求了。

分享到:
评论

相关推荐

    spring-web-2.5.jar

    org.springframework.web.context.ContextLoaderServlet.class org.springframework.web.context.ServletConfigAware.class org.springframework.web.context.ServletContextAware.class org.springframework.web....

    在Eclipse 中创建Spring的 Web应用.doc

    3. **配置Spring启动方式**: 在`web.xml`中,你需要配置`ContextLoaderListener`或`ContextLoaderServlet`。例如: - 使用监听器方式: ```xml &lt;listener-class&gt;org.springframework.web.context....

    加载spring 文件,在web.xml中的配置

    因此,一般情况下,我们只选择其中一种方式来加载Spring配置。 5. **自定义配置** 除了默认的`/WEB-INF/applicationContext.xml`,你还可以通过`contextConfigLocation`指定多个配置文件,用逗号分隔它们,例如`...

    Spring学习资料

    Spring通过DI实现了组件间的松耦合,具体包括接口注入、setter方法注入和构造方法注入三种方式。接口注入是将依赖的实现类作为接口的实现注入到需要它的对象中;setter注入是通过setter方法设置依赖对象;构造方法...

    使用MyEclipse集成SSH和DWR【最佳方案】

    - 当使用Struts Plugin方式时,应同时使用`ContextLoaderListener`或`ContextLoaderServlet`来确保Spring配置文件能够被正确加载。 - 最佳实践建议:使用Struts Plugin来加载`action-servlet.xml`,而通过`...

    Spring中ApplicationContext加载机制

    首先,Spring 提供了两种选择来加载 ApplicationContext:ContextLoaderListener 和 ContextLoaderServlet。这两者在功能上完全等同,只是一个是基于 Servlet2.3 版本中新引入的 Listener 接口实现,而另一个基于 ...

    struts 整合spring 例子,测试通过

    首先,Spring框架提供了一种灵活的方式来管理应用程序的bean,包括创建、配置和依赖注入。而Struts2则是一个流行的MVC框架,它处理HTTP请求并负责视图和控制器的交互。两者结合可以使业务逻辑和表现层分离得更加清晰...

    spring配置.txtspring配置.txt

    Spring框架支持自动装配,这是一种简化依赖注入的方式。在XML配置中,可以通过`autowire`属性来指定bean之间的依赖关系。具体来说: - **No**:不启用自动装配。这是默认值,需要手动指定依赖注入。 - **By Name**...

    spring3.x的读书笔记3

    在Spring 3.x版本中,WebApplicationContext的初始化是通过Web容器来完成的,主要有两种方式:使用ContextLoaderListener或ContextLoaderServlet。 1. **ContextLoaderListener启动WebApplicationContext** ...

    配置spring

    Spring是一个广泛使用的Java应用程序框架,它提供了一种模块化和松耦合的方式来组织和管理应用组件。Struts则是一个用于构建MVC(模型-视图-控制器)架构的Java Web框架,而iBatis是一个SQL映射框架,它允许开发者将...

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

    本篇文章将深入探讨Spring在`web.xml`中与在Struts中的不同配置方式,以及这两种方式背后的设计思想。 首先,ApplicationContext是Spring的核心组件,它是一个容器,负责管理和装配应用程序中的Bean。在Web应用中,...

    spring源代码解析

    Spring的ContextLoader是提供这样性能的类,我们可以使用 ContextLoaderServlet或者ContextLoaderListener的启动时载入的Servlet来实例化Spring IOC容器 – 为什么会有两个不同的类来装载它呢,这是因为它们的使用...

    spring在web.xml中和在struts中的不同配置..pdf

    在Java Web开发中,Spring和Struts是两个非常流行的框架,它们在构建应用程序时有着不同的配置方式。...这两种方式各有优劣,但都体现了Spring的灵活性和可扩展性,可以根据项目需求选择合适的集成方式。

    spring中的所有配置

    除了`ContextLoaderListener`,Spring还提供了另一种方式来初始化WebApplicationContext,那就是通过`ContextLoaderServlet`。这是一种Servlet,它同样可以在`web.xml`中配置,通常用于处理特定的URL模式,并在启动...

    ssh struts+hibernate+spring

    3. **使用ContextLoaderServlet**:另一种加载Spring上下文的方法,同样在web.xml中配置,`ContextLoaderServlet`会在Web应用启动时加载Spring配置。 **Spring事务管理** 的配置主要涉及到`...

    spring与axis的整合

    Spring与Axis的整合,是指在Java环境中利用Spring框架管理和配置Axis Web服务的一种技术。这种整合方式充分利用了Spring框架的强大功能(如依赖注入、面向切面编程等)以及Axis作为高效Web服务引擎的能力。通过这种...

    Spring的监听器ContextLoaderListener的作用

    ContextLoaderListener 是 Spring 框架中的一种监听器,它的主要作用是启动 Web 容器时,自动装配 ApplicationContext 的配置信息。它实现了 ServletContextListener 接口,在 web.xml 文件中配置这个监听器,启动...

    spring与struts2整合

    2. 第二种方法是使用Spring插件,让Struts2直接从Spring容器中获取Action实例。在这种情况下,Struts2配置文件中的`class`属性不再指定具体类,而是Spring Bean的ID: ```xml &lt;result name="success"&gt;/index....

    spring的ppt

    在Spring中,Bean的生命周期包括初始化、正常使用和销毁三个阶段。在BeanFactory中,可以通过实现InitializingBean接口或定义init-method属性来指定初始化方法;在ApplicationContext中,除了初始化方法,还有更多的...

Global site tag (gtag.js) - Google Analytics