SSM:spring+springmvc+mybatis框架中的XML配置檔案功能詳細解釋
SSM:spring+springmvc+mybatis框架中的XML配置檔案功能詳細解釋
這幾天一直在整合SSM框架,雖然網上有很多已經整合好的,但是對於裡面的配置檔案並沒有進行過多的說明,很多人知其然不知其所以然,經過幾天的搜尋和整理,今天總算對其中的XML配置檔案有了一定的瞭解,所以拿出來一起分享一下,希望有不足的地方大家批評指正~~~
首先 這篇文章暫時只對框架中所要用到的配置檔案進行解釋說明,而且是針對註解形式的,框架運轉的具體流程過兩天再進行總結.
spring+springmvc+mybatis框架中用到了三個XML配置檔案:web.xml,spring-mvc.xml,spring-mybatis.xml.第一個不用說,每個web專案都會有的也是關聯整個專案的配置.第二個檔案spring-mvc.xml是springmvc的一些相關配置,第三個是mybatis的相關配置.
專案中還會用到兩個資源屬性檔案jdbc.properties和log4j.properties.一個是關於jdbc的配置,提取出來方便以後的修改.另一個是日誌檔案的配置.
以上是我這篇文章中所要講的內容,比較簡單,也很容易懂.希望大牛不要鄙視~~接下來進入正題:
一 web.xml
關於這個配置檔案我以前一直都是朦朦朧朧的狀態,剛好藉著這次整合框架的機會將它瞭解清楚.在下面的程式碼中我對每一個標籤都進行了詳細的註釋,大家一看就懂,主要理解<servlet>中的配置,因為其中配置了前端控制器,在SSM框架中,前端控制器起著最主要的作用.下面貼上程式碼
<?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_3_0.xsd" version="3.0"> <context-param> <!--全域性範圍內環境引數初始化--> <param-name>contextConfigLocation</param-name> <!--引數名稱--> <param-value>classpath:spring-mybatis.xml</param-value> <!--引數取值--> </context-param> <!--以下配置的載入順序:先 ServletContext >> context-param >> listener >> filter >> servlet >> spring--> <!---------------------------------------------------過濾器配置------------------------------------------------------> <!--例:編碼過濾器--> <filter> <!-- 用來宣告filter的相關設定,過濾器可以擷取和修改一個Servlet或JSP頁面的請求或從一個Servlet或JSP頁面發出的響應--> <filter-name>encodingFilter</filter-name> <!--指定filter的名字--> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--定義filter的類的名稱--> <async-supported>true</async-supported> <!--設定是否啟用非同步支援--> <init-param><!--用來定義引數,若在Servlet可以使用下列方法來獲得:String param_name=getServletContext().getInitParamter("param-name裡面的值");--> <param-name>encoding</param-name> <!--引數名稱--> <param-value>UTF-8</param-value> <!--引數值--> </init-param> </filter> <filter-mapping><!--用來定義filter所對應的URL--> <filter-name>encodingFilter</filter-name> <!--指定對應filter的名字--> <url-pattern>/*</url-pattern> <!--指定filter所對應的URL--> </filter-mapping> <!---------------------------------------------------監聽器配置------------------------------------------------------> <!--例:spring監聽器--> <listener> <!--用來設定Listener介面--> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!--定義Listener的類名稱--> </listener> <!-- 防止Spring記憶體溢位監聽器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!---------------------------------------------------servlet配置------------------------------------------------------> <servlet> <!--用來宣告一個servlet的資料 --> <servlet-name>SpringMVC</servlet-name> <!--指定servlet的名稱--> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--指定servlet的類名稱,這裡配置了前端控制器--> <init-param><!--用來定義引數,可有多個init-param。在servlet類中通過getInitParamenter(String name)方法訪問初始化引數 --> <param-name>contextConfigLocation</param-name> <!--引數名稱--> <param-value>classpath:spring-mvc.xml</param-value> <!--引數值--> </init-param> <load-on-startup>1</load-on-startup><!--當值為正數或零時:Servlet容器先載入數值小的servlet,再依次載入其他數值大的servlet.--> <async-supported>true</async-supported><!--設定是否啟用非同步支援--> </servlet> <servlet-mapping><!--用來定義servlet所對應的URL--> <servlet-name>SpringMVC</servlet-name> <!--指定servlet的名稱--> <url-pattern>/</url-pattern> <!--指定servlet所對應的URL--> </servlet-mapping> <!-----------------------------------------------會話超時配置(單位為分鐘)-------------------------------------------------> <session-config><!--如果某個會話在一定時間未被訪問,則伺服器可以扔掉以節約記憶體--> <session-timeout>120</session-timeout> </session-config> <!---------------------------------------------------MIME型別配置 ------------------------------------------------------> <mime-mapping><!--設定某種副檔名的檔案用一種應用程式來開啟的方式型別,當該副檔名檔案被訪問的時候,瀏覽器會自動使用指定應用程式來開啟--> <extension>*.ppt</extension> <!--副檔名名稱--> <mime-type>application/mspowerpoint</mime-type> <!--MIME格式--> </mime-mapping> <!---------------------------------------------------歡迎頁面配置 ------------------------------------------------------> <welcome-file-list><!--定義首頁列單.--> <welcome-file>/index.jsp</welcome-file> <!--用來指定首頁檔名稱.我們可以用<welcome-file>指定幾個首頁,而伺服器會依照設定的順序來找首頁.--> <welcome-file>/index.html</welcome-file> </welcome-file-list> <!---------------------------------------------------配置錯誤頁面------------------------------------------------------> <error-page> <!--將錯誤程式碼(Error Code)或異常(Exception)的種類對應到web應用資源路徑.--> <error-code>404</error-code> <!--HTTP Error code,例如: 404、403--> <location>error.html</location> <!--用來設定發生錯誤或異常時要顯示的頁面--> </error-page> <error-page> <exception-type>java.lang.Exception</exception-type><!--設定可能會發生的java異常型別,例如:java.lang.Exception--> <location>ExceptionError.html</location> <!--用來設定發生錯誤或異常時要顯示的頁面--> </error-page> </web-app>
二 spring-mvc.xml
需要實現基本功能的配置
1 配置 <mvc:annotation-driven/>
2 配置 <context:component-scan base-package="com.springmvc.controller"/> //配置controller的注入
3 配置檢視解析器
<mvc:annotation-driven/> 相當於註冊了DefaultAnnotationHandlerMapping(對映器)和AnnotationMethodHandlerAdapter(介面卡)兩個bean.即解決了@Controller註解的使用前提配置。
context:component-scan 對指定的包進行掃描,實現註釋驅動Bean定義,同時將bean自動注入容器中使用。即解決了@Controller標識的類的bean的注入和使用。
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 1、配置對映器與介面卡 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 2、檢視解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<span style="white-space:pre"> </span><!-- 字首和字尾 -->
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 3、自動掃描該包,使SpringMVC認為包下用了@controller註解的類是控制器 -->
<context:component-scan base-package="com.rhzh.controller"/>
</beans>
三 spring-mybatis.xml
需要實現基本功能的配置
1 配置 <context:component-scan base-package="com.rhzh"/> //自動掃描,將標註Spring註解的類自動轉化Bean,同時完成Bean的注入
2 載入資料資源屬性檔案
3 配置資料來源 三種資料來源的配置方式 http://blog.csdn.net/yangyz_love/article/details/8199207
4 配置sessionfactory
5 裝配Dao介面
6 宣告式事務管理
7 註解事務切面
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!--1 自動掃描 將標註Spring註解的類自動轉化Bean-->
<context:component-scan base-package="com.rhzh" />
<!--2 載入資料資源屬性檔案 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<span style="white-space:pre"><!-- 3 配置資料來源 --></span>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化連線大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 連線池最大數量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 連線池最大空閒 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 連線池最小空閒 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 獲取連線最大等待時間 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- 4 配置sessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自動掃描mapping.xml檔案 -->
<property name="mapperLocations" value="classpath:com/rhzh/mapping/*.xml"></property>
</bean>
<!-- 5 裝配dao介面 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.rhzh.dao" /> <!-- DAO介面所在包名,Spring會自動查詢其下的類 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 6、宣告式事務管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 7、註解事務切面 --><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"> <tx:annotation-driven transaction-manager="transactionManager"/>
下面是需要引入的兩個資源屬性檔案:
jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ecdatabase?characterEncoding=utf-8
username=root
password=admin
#定義初始連線數
initialSize=0
#定義最大連線數
maxActive=20
#定義最大空閒
maxIdle=20
#定義最小空閒
minIdle=1
#定義最長等待時間
maxWait=60000
log4j.properties
#定義LOG輸出級別
log4j.rootLogger=INFO,Console,File
#定義日誌輸出目的地為控制檯
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以靈活地指定日誌輸出格式,下面一行是指定具體的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#檔案大小到達指定尺寸的時候產生一個新的檔案
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定輸出目錄
log4j.appender.File.File = logs/ssm.log
#定義檔案最大大小
log4j.appender.File.MaxFileSize = 10MB
# 輸出所以日誌,如果換成DEBUG表示輸出DEBUG以上級別日誌
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
以上僅僅是對於框架中的配置檔案進行解釋說明,整合框架並不是能夠執行就OK了,瞭解其中的原理以便於以後發現問題和解決問題,不要急於求成,一步一步走踏實了,你會發現你比別人走的更快!!!
*本文章轉載自 yijiemamin 的部落格