zoukankan      html  css  js  c++  java
  • ssm框架搭建流程及原理分析

        这几天自己想搭建个ssm框架玩一下,有些东西长时间不玩都给忘了,所以自己把整个流程整理了一下,只要跟着步骤,就能顺利完成ssm框架的搭建。

    一、搭建步骤:   

         1.整理jar包
         2.对于一个web工程,程序的运行是从web.xml文件中开始读取,因些我们需要先从web.xml文件配置
         3.web.xml文件的配置  

     1  <?xml version="1.0" encoding="UTF-8"?>
     2     <web-app
     3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4         xmlns="http://java.sun.com/xml/ns/javaee"
     5         xsi:schemaLocation="
            http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 6 <display-name>02-ssm</display-name> 7 <!-- 1. 加载Spring容器配置 --> 8 <!-- 1.1 配置ContextLoaderListener 监听器 --> 9 作用:ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法 --> 10 <listener> 11 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 12 </listener> 13 <!-- 1.2 设置Spring容器加载所有的配置文件的路径 --> 14 <context-param> 15 <param-name>contextConfigLocation</param-name> 16 <param-value>classpath:config/spring/applicationContext.xml</param-value> 17 </context-param> 18 19 <!-- 2.配置SpringMVC核心控制器 --> 20 <servlet> 21 <!-- 2.1配置SpringMVC的前端控制器 --> 22 <servlet-name>springmvc</servlet-name> 23 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 24 <!-- 2.2配置前端控制器的路径 --> 25 <init-param> 26 <param-name>contextConfigLocation</param-name> 27 <param-value>classpath:config/springmvc/springmvc.xml</param-value> 28 </init-param> 29 <!-- 2.3启动加载一次 --> 30 <load-on-startup>1</load-on-startup> 31 </servlet> 32 <!-- 2.4.为DispatcherServlet建立映射 --> 33 <servlet-mapping> 34 <servlet-name>springmvc</servlet-name> 35 <!-- 2.4.1此处可以可以配置成*.do --> 36 <url-pattern>*.do</url-pattern> 37 </servlet-mapping> 38 39 <!-- 3.解决工程编码过滤器 --> 40 <filter> 41 <filter-name>encodingFilter</filter-name> 42 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 43 <init-param> 44 <param-name>encoding</param-name> 45 <param-value>UTF-8</param-value> 46 </init-param> 47 <init-param> 48 <param-name>forceEncoding</param-name> 49 <param-value>true</param-value> 50 </init-param> 51 </filter> 52 <filter-mapping> 53 <filter-name>encodingFilter</filter-name> 54 <url-pattern>/*</url-pattern> 55 </filter-mapping> 56 </web-app>


        4.按照加载的顺序,第1个会先初始化spring容器,配置spring的配置文件applicationContext.xml       

     1  <?xml version="1.0" encoding="UTF-8"?>
     2         <beans xmlns="http://www.springframework.org/schema/beans"
     3             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
     4             xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache"
     5             xmlns:task="http://www.springframework.org/schema/task"
     6             xmlns:context="http://www.springframework.org/schema/context"
     7             xsi:schemaLocation="http://www.springframework.org/schema/beans
     8                 http://www.springframework.org/schema/beans/spring-beans.xsd
     9                 http://www.springframework.org/schema/context  
    10                 http://www.springframework.org/schema/context/spring-context.xsd  
    11                 http://www.springframework.org/schema/aop
    12                 http://www.springframework.org/schema/aop/spring-aop.xsd
    13                 http://www.springframework.org/schema/tx
    14                 http://www.springframework.org/schema/tx/spring-tx.xsd
    15                 http://www.springframework.org/schema/cache
    16                 http://www.springframework.org/schema/cache/spring-cache.xsd
    17                 http://www.springframework.org/schema/task
    18                 http://www.springframework.org/schema/task/spring-task.xsd">
    19                 
    20             <!--1. 注解扫描包 -->
    21             <context:component-scan base-package="com.ssm.*">
    22                 <!-- 过滤掉控制器注解 -->
    23                 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    24             </context:component-scan>
    25             <!-- ***** 在容器初始化时,创建对象 可以添加自己需要的配置 ***** -->
    26             
    27             <!-- 2.加载数据源配置文件(可加载多个,此处的配置是添加多个) -->
    28             <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    29                 <property name="locations">
    30                     <list>
    31                         <value>classpath:config/jdbc.properties</value>
    32                     </list>
    33                 </property>
    34             </bean>
    35             
    36             <!-- 3.定义一个使用的DBCP实现的数据源 -->
    37             <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    38                 destroy-method="close">
    39                 <property name="driverClassName" value="${jdbc.driverClassName}" />
    40                 <property name="url" value="${jdbc.url}" />
    41                 <property name="username" value="${jdbc.username}" />
    42                 <property name="password" value="${jdbc.password}" />
    43             </bean>
    44             
    45             <!-- 4.spring 与 mybatis的整合,加载mybatis配置文件 -->
    46             <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    47                 <property name="dataSource" ref="dataSource"></property>
    48                 <property name="configLocation" value="classpath:config/mybatis/mybatis-config.xml" />
    49             </bean>
    50             
    51             <!--  5. mybatis自动扫描加载SqlMapper映射文件  -->
    52             <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    53                 <property name="basePackage" value="com.ssm"></property>
    54                 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    55             </bean>
    56             
    57             <!--6. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源-->
    58             <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    59                 <property name="dataSource" ref="dataSource"></property>
    60             </bean>
    61             
    62             <!-- 7. 使用声明式事务 transaction-manager:引用上面定义的事务管理器-->
    63             <tx:annotation-driven transaction-manager="txManager" />
    64          </beans>

        5.加载第2个springmvc容器,配置springmvc.xml文件的配置     

     1   <?xml version="1.0" encoding="UTF-8"?>
     2          <beans xmlns="http://www.springframework.org/schema/beans"
     3             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
     4             xmlns:mvc="http://www.springframework.org/schema/mvc"
     5             xsi:schemaLocation="http://www.springframework.org/schema/beans
     6             http://www.springframework.org/schema/beans/spring-beans.xsd
     7             http://www.springframework.org/schema/context
     8             http://www.springframework.org/schema/context/spring-context-4.0.xsd
     9             http://www.springframework.org/schema/mvc
    10             http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    11 
    12             <!-- 注解扫描包 -->
    13             <context:component-scan base-package="com.ssm.*" />
    14             <!-- 开启注解 -->
    15             <mvc:annotation-driven></mvc:annotation-driven>  
    16             
    17             <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    18             <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    19                 <!-- 这里的配置我的理解是自动给后面do的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
    20                 <property name="prefix" value="/WEB-INF/jsp" />
    21                 <property name="suffix" value=".jsp" />
    22             </bean>
    23         </beans>

    二、原理分析   

      相信你配置完了,肯定想弄清原理是怎么回事,下面我们一起来分析一下:
        1.web.xml中的配置分析
             (1).spring容器的初始化
                 spring容器在初始化时,配置ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息,容器后启动就会默认执行它实现的方法.
                 通过查看源码知道,在ContextLoaderListener继承ContextLoader类,并实现ServletContextListener,而在整个加载配置过程由ContextLoader来完成,因为它实现了
                 ServletContextListener这个接口,在 Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期。
                当Servlet 容器启动或终止Web 应用时,会触发ServletContextEvent 事件,该事件由 ServletContextListener 来处理。
                在 ServletContextListener 接口中定义了处理ServletContextEvent 事件的两个方法。
                l.contextInitialized(ServletContextEvent sce) :当Servlet 容器启动Web 应用时调用该方法。在调用完该方法之后,容器再对Filter 初始化,
                    并且对那些在Web 应用启动时就需要被初始化的Servlet 进行初始化。
                2.contextDestroyed(ServletContextEvent sce) :当Servlet 容器终止Web 应用时调用该方法。在调用该方法之前,容器会先销毁所有的Servlet 和Filter 过滤器。
                
             (2).springmvc容器的初始化
                 springmvc容器在初始化容器时,在web.xml中配置了DispatcherServlet前端控制器,它支撑了所有的访问,并负责职责的分派.每一次访问DispatchServlet会有一个自己的上下文,
                 称为子上下文, 它也保存在 ServletContext中,key 是"org.springframework.web.servlet.FrameworkServlet.CONTEXT"+Servlet名称。当一 个Request对象产生时,
                 会把这个子上下文对象(WebApplicationContext)保存在Request对象中,key是 DispatcherServlet.class.getName() + ".CONTEXT"。WebApplicationContext继承自ApplicationContext,
                 它们的的初始化方式还是有所不同的,WebApplicationContext需要ServletContext实例,也就是说它必须拥有Web容器的前提下才能完成启动的工作,.有过Web开发经验的读者都知道可以在
                 web.xml中配置自启动的Servlet或定义Web容器监听器(ServletContextListener),借助着两者中的任何一个,我们就可以启动Spring Web应用上下文的工作.而在这里,我们创建的是springmvc容器
            
             (3).创建spring 容器和springmvc容器区别
                 在ssm框架中创建了这2个容器,spring属于父容器,在进行注解扫描时主要对service层、dao层的bean进行扫描和管理,而springmvc主要是对controller层的bean进行扫描和管理的,子容器可以使用
                 父容器中的对象,而父容器不能使用子容器中的对象.

        2.applicationContext.xml 中的配置分析

        3.springmvc.xml 中的配置分析

  • 相关阅读:
    day 66 ORM django 简介
    day 65 HTTP协议 Web框架的原理 服务器程序和应用程序
    jQuery的事件绑定和解绑 事件委托 轮播实现 jQuery的ajax jQuery补充
    background 超链接导航栏案例 定位
    继承性和层叠性 权重 盒模型 padding(内边距) border(边框) margin 标准文档流 块级元素和行内元素
    属性选择器 伪类选择器 伪元素选择器 浮动
    css的导入方式 基础选择器 高级选择器
    03-body标签中相关标签
    Java使用内存映射实现大文件的上传
    正则表达式
  • 原文地址:https://www.cnblogs.com/MrRightZhao/p/7851565.html
Copyright © 2011-2022 走看看