• SSH--三大框架整合原理


    https://blog.csdn.net/u014010769/article/details/44993533

    三大框架整合原理
    1、三大框架的作用
    struts2是一个mvc框架
    spring容器
    1、利用ioc和di做到了完全的面向接口编程
    2、由于spring的声明式事务处理,使程序员不再关注事务
    3、dao层和service层的类是单例的,但是action层是多例
    hibernate
    就是一个数据库的ormapping的框架
    2、整合原理
    1、当tomcat启动时,做的事情
    因为在web.xml中,
                     <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>
                     <filter>
                         <filter-name>struts2</filter-name>
                         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
                     </filter>
                     <filter-mapping>
                          <filter-name>struts2</filter-name>
                          <url-pattern>/*</url-pattern>
                     </filter-mapping>
    所以在启动的时候,执行的是
                     ContextLoaderListener contextInitialized this.contextLoader =createContextLoader();
    加载spring的配置文件,这里有一个固定的参数con的textConfigLocation,可以指定classpath路径下的spring的配置文件,也可以任意位置指定配置文件  spring*.xml    WEB-INF/任意多个任意文件夹/spring-*.xml
    如果没有指定固定参数,则查找默认的加载路径:WEB-INF/applicationContext.xml
    this.contextLoader.initWebApplicationContext(event.getServletContext());
    启动spring容器
         总结:当tomcat启动的时候,spring容器就启动了,这个时候service层和dao层所有的单例类就创建对象了
    struts2容器:
         加载了default.properties,struts-default.xml,struts-plugin.xml,struts.xml
       2、请求一个url时,发生的事情:
            1、在引入jar包时,导入了struts2-spring-plugin-2.1.8.1.jar包,该jar中有一个文件struts-plugin.xml
                    <beantype="com.opensymphony.xwork2.ObjectFactory" name="spring"
                         class="org.apache.struts2.spring.StrutsSpringObjectFactory"/>
                    <constantname="struts.objectFactory" value="spring" />
            2、由于上面的配置改变了action的生成方式,action由StrutsSpringObjectFactory生成,经过查找是由SpringObjectFactory中的buidBean方法生成的
                             try {
                                  o =appContext.getBean(beanName);
                              } catch(NoSuchBeanDefinitionException e) {
                                  Class beanClazz =getClassInstance(beanName);
                                  o =buildBean(beanClazz, extraContext);
                             }
    3、由上面的代码可以看出,先从spring容器中查找相应的action,如果没有找到,再根据反射机制创建action,beanName就是struts配置文件class属性的值,所以class属性的值和spring中ID的值保持一致

  • 相关阅读:
    5.16欢乐赛
    卢卡斯定理
    noip2017 宝藏
    [JSOI2009]计数问题
    HDU 1160 FatMouse's Speed
    HDU 1260 Tickets
    HDU 1176 免费馅饼
    HDU 1114 Piggy-Bank
    HDU 1074 Doing Homework
    HDU 1069 Monkey and Banana
  • 原文地址:https://www.cnblogs.com/arrows/p/10511428.html
Copyright © 2020-2023  润新知