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 中的配置分析
转载于:https://www.cnblogs.com/MrRightZhao/p/7851565.html
ssm框架搭建流程及原理分析相关推荐
- 最完整的SSM框架搭建流程
1.新建一个Maven项目 这里以Idea工具为例,打开IDEA,然后File->New->Project ->Maven->Create from archetype. 2. ...
- 【转】SSM框架搭建流程与解析
SpringMVC部分 1 引入Jar spring-context spring-webmvc 2 web.xml中 配置 核心拦截器 DispatchServlet <servlet& ...
- 支付宝app支付java后台流程、原理分析(含nei wang chuan tou)
java版支付宝app支付流程及原理分析 本实例是基于springmvc框架编写 一.流程步骤 1.执行流程 当手机端app(就是你公司开发的app) ...
- SSM框架搭建详细解析
总结了一下搭建SSM框架流程,在以后用到的时候方便回头使用. 使用工具:MyEclipse 2015:Tomcat 8版本:jdk1.8版本. 首先: 1:创建一个WebProject项目,jdk1. ...
- SSM框架搭建,及遇到的问题
SSM框架搭建,及遇到的问题 1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Exp ...
- 基于 SSM 框架搭建的宠物领养系统
近年来,随着人们对于宠物领养.保护的意识日益提高,宠物领养市场逐渐走向规范化.高效化.而针对这一市场需求,基于 SSM 框架搭建的宠物领养系统应运而生. 一.关于 SSM 框架 SSM 是一个基于 S ...
- SSM框架搭建学习总结
SSM框架搭建学习总结 #开发环境 1.操作系统:windows 2.web服务器:Tomcat8.0 3.Java开发包:JDK8 4.开发工具:MyEclipse或者Eclipse都可以 5.数据 ...
- 小菜鸟的SSM框架搭建【详细步骤】【SSM/IDEA/MAVEN】
小菜鸟的SSM框架搭建 内容很长噢,一步步搭建 此框架是跟着b站上的黎曼的猜想所发布的视频搭建起来的,细节操作可以看视频.我只是在这里梳理一下ssm框架搭建的流程. 整合说明:SSM整合可以使用多种方 ...
- SSM框架搭建(四) springmvc和mybatis的配置
SSM框架搭建(一) JDK和MAVEN环境搭建 SSM框架搭建(二) 创建MAVEN项目 SSM框架搭建(三) 数据库创建和MyBatis生成器自动生成实体类.DAO接口和Mapping映射文件 S ...
最新文章
- linux下给php安装pdo_mysql和zlib以及测试模式的选择(禅道安装过程中遇到的问题)...
- OpenStack Nova Release(Rocky to Train)
- ComponentOne Ultimate 2020中文版
- Android中实现照片滑动时左右进出的动画的xml代码
- 最优传输理论和生成模型的几何观点
- 深入理解javascript原型和闭包(14)——从【自由变量】到【作用域链】
- MySQL常见问题的解决,root用户密码忘记,不是内部或外部命令,修改数据库和表的字符编码,命令行客户端的字符集问题
- java学习(173):class反射编程方法
- Apache ab并发负载压力测试
- Knockout事件传递参数的几种方式
- 怎样和处在“叛逆”阶段的孩子交流沟通?
- ret2dir:Rethinking Kernel Isolation(翻译)
- 嵌入式系统——电子设计
- matlab difittool,matlab工具箱下载安装和使用方法的汇总|Toolbox 大全
- PS学习笔记-----提示暂存盘满了怎么办???
- cesium之三维漫游飞行效果实现篇(转)
- 手动查毒删除病毒文件图
- 组件化与插件化的差别在哪里?醍醐灌顶!
- 高速公路ETC卡签之我见1-概述
- 阿里云视频点播-对接注意点