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

一、搭建步骤:

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

  <?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
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">
<display-name>02-ssm</display-name>
<!-- 1. 加载Spring容器配置 -->
<!-- 1.1 配置ContextLoaderListener 监听器 -->
作用:ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 1.2 设置Spring容器加载所有的配置文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/applicationContext.xml</param-value>
</context-param> <!-- 2.配置SpringMVC核心控制器 -->
<servlet>
<!-- 2.1配置SpringMVC的前端控制器 -->
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 2.2配置前端控制器的路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/springmvc/springmvc.xml</param-value>
</init-param>
<!-- 2.3启动加载一次 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 2.4.为DispatcherServlet建立映射 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 2.4.1此处可以可以配置成*.do -->
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- 3.解决工程编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

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

  <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd"> <!--1. 注解扫描包 -->
<context:component-scan base-package="com.ssm.*">
<!-- 过滤掉控制器注解 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- ***** 在容器初始化时,创建对象 可以添加自己需要的配置 ***** --> <!-- 2.加载数据源配置文件(可加载多个,此处的配置是添加多个) -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/jdbc.properties</value>
</list>
</property>
</bean> <!-- 3.定义一个使用的DBCP实现的数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <!-- 4.spring 与 mybatis的整合,加载mybatis配置文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:config/mybatis/mybatis-config.xml" />
</bean> <!-- 5. mybatis自动扫描加载SqlMapper映射文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean> <!--6. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 7. 使用声明式事务 transaction-manager:引用上面定义的事务管理器-->
<tx:annotation-driven transaction-manager="txManager" />
</beans>

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

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

ssm框架搭建流程及原理分析的更多相关文章

  1. SSM框架搭建web服务器实现登录功能(Spring+SpringMVC+Mybatis)

    初学java EE,虽然知道使用框架会使开发更加便捷高效,但是对于初学者来说,感到使用框架比较迷惑,尤其是各种jar包的引用.各种框架的配置.注解的使用等等. 最好的学习方法就是实践,于是下载了一个现 ...

  2. 实习小结(二)--- SSM框架搭建

    SSM项目框架搭建 前几天做了一个学生信息管理的项目,使用纯控制台输入,查询数据库,将信息在控制台中打印,功能完善得差不多之后,老师让将这个项目移植到Web中,使用Spring+SpringMVC+M ...

  3. SSM框架搭建教程(从零开始,图文结合)

    1.准备 IntelliJ IDEA Tomcat JDK Maven mysql spring.springmvc.mybatis 了解 现在假设如上条件你都具备,那么通过我这篇博客 你一定可以整合 ...

  4. 支付宝app支付java后台流程、原理分析(含nei wang chuan tou)

    java版支付宝app支付流程及原理分析 本实例是基于springmvc框架编写     一.流程步骤         1.执行流程           当手机端app(就是你公司开发的app)在支付 ...

  5. SSM框架搭建详细解析

    总结了一下搭建SSM框架流程,在以后用到的时候方便回头使用. 使用工具:MyEclipse 2015:Tomcat 8版本:jdk1.8版本. 首先: 1:创建一个WebProject项目,jdk1. ...

  6. SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>

    此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...

  7. SSM 框架搭建

    SSM框架搭建(Spring.SpringMVC.Mybatis) 一:基本概念 Spring :      Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框 ...

  8. SpringMVC笔记——SSM框架搭建简单实例

    落叶枫桥 博客园 首页 新随笔 联系 订阅 管理 SpringMVC笔记——SSM框架搭建简单实例 简介 Spring+SpringMVC+MyBatis框架(SSM)是比较热门的中小型企业级项目开发 ...

  9. idea ssm框架搭建

    1.分享一篇完整的ssm框架搭建连接 大牛博客:https://www.cnblogs.com/toutou/p/ssm_spring.html#_nav_0 2.我的搭建的完整项目连接,可以进入我的 ...

随机推荐

  1. SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 4)

    (六)高效合理的使用LINQ 1.DataContext中的两个属性 为了能够使用DataContext进行数据提交,在DataContext进行数据查询和操作的过程中,内部会进行数据状态的保持和追踪 ...

  2. 常用的mysql语句

    为了方便学习mysql,把接触到的sql收集一下,忘记的时候可以查询一下. 连接mysql数据库: mysql -u 用户名 -p 输入密码. 创建数据库: create database 数据库名; ...

  3. REST Representational state transfer REST Resource Naming Guide Never use CRUD function names in URIs

    怎样用通俗的语言解释什么叫 REST,以及什么是 RESTful? - 知乎  https://www.zhihu.com/question/28557115 大家都知道"古代"网 ...

  4. Web 编程中路径问题

    web.xml 中 <url-pattern> 路径(即 Servlet 路径) 要么以 "*" 开头, 要么以 "/" 开头. 转发和包含路径(服 ...

  5. pip安装lxml报错 Fatal error in launcher: Unable to create process using '"c:\users\administrator\appdata\local\programs\python\python36\python.exe" "C:\Users\Administrator\AppData\L

    pip install lxml 安装报错 E:\apollo\spider_code>Fatal error in launcher: Unable to create process usi ...

  6. 我的Android进阶之旅------>解决Jackson等第三方转换Json的开发包在开启混淆后转换的实体类数据都是null的bug

    1.错误描述 今天测试人员提了一个bug,说使用我们的app出现了闪退的bug,后来通过debug断点调试,发现我们的app转换服务器发送过来的json数据后,都是为null.而之前已经提测快一个月的 ...

  7. 几分钟私人定制APP全攻略!!

    上网百度了一下什么是自媒体,你会看到这种介绍:自媒体(外文名:We Media)又称"公民媒体"或"个人媒体",是指私人化.平民化.普泛化.自主化的传播者,以现 ...

  8. 003-Java非堆CodeCache详解

    一.概述 Java的内存由堆和非堆两个部分组成.对于堆来说,它的组成是比较确定的,它包含了年轻代和年老代两个部分,而年轻代又是由Eden区和两个Survivor区组成.可是,非堆由哪些部分组成呢? 在 ...

  9. composer 常用包管理工具

    名称 用途说明 说明地址 mashape/unirest-php 简单易用的HTTP请求库 官网地址 guzzlehttp/guzzle 功能强大的HTTP请求库 文档 hassankhan/conf ...

  10. HDU1069:Monkey and Banana(最长上升子序列的应用)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1069 这题挺简单的,给定一个箱子的长宽高,要求啰箱子,但必须保证下面箱子的长和宽必须大于上面的箱子. 一个 ...