WEB启动时,如何加载spring容器和struts2容器(bean的注入和url如何请求)

整合原理

整合原理:
   *  web.xml
      *  spring容器是以监听器的形式与tomcat整合的
           <listener>
                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
           </listener>
           <context-param>
                 <param-name>contextConfigLocation</param-name>
                 <param-value>classpath:spring/applicationContext.xml</param-value>
           </context-param>
      *  以过滤器的形式整合struts2容器
      *    <filter>
                <filter-name>struts2</filter-name>
                <filter-class>             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
                </filter-class>
            </filter>

ContextLoaderListener 说明:

  • ContextLoaderListener 当中的createContextLoader()加载spring的web容器
  • initWebApplicationContext
    * 初始化spring的web容器
    * 加载其配置文件
  • 当执行完这两个方法以后,就启动spring的web容器了,在spring容器中,单例模式的
    bean就被实例化了,所以dao和service层的对象和代理对象就在这个时候产生了

这里写图片描述


在tomcat启动的时候,干了两件事情:

  • 加载了各种配置文件
  • 静态注入了一些bean

这里写图片描述


当发出 url 请求时 请求url:personAction_savePerson.action

步骤
* 先找struts的配置文件,会找根据struts2的相关配置查找action的创建方式
* 会去常量struts.objectFactory查找到底是由哪个类创建了action
* 会去struts-default.xml,struts-plugin.xml,struts.xml文件去找struts.objectFactory
* 哪个配置文件加载在最后,哪个决定
* 最后在struts和spring整合的包中找到了struts-plugin.xml文件
* <bean type="com.opensymphony.xwork2.ObjectFactory"
* name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constant name="struts.objectFactory" value="spring" />

* 由上述的内容可以知道,action是由StrutsSpringObjectFactory创建的,而该类继承了SpringObjectFactory

这里写图片描述

该源代码中beanName就是struts2配置文件中的action元素的class属性的值,这就意味着
class属性的值要和spring容器中action所在的bean所指定的id值要一致
如下图
这里写图片描述


这里写图片描述

发布了30 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/wsl9420/article/details/53350045
今日推荐