這幾天自己想搭建個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 中的配置分析
