1. 程式人生 > >SSM:spring+springmvc+mybatis框架中的XML配置文件功能詳細解釋

SSM:spring+springmvc+mybatis框架中的XML配置文件功能詳細解釋

con initial -m and 整理 .get 尺寸 internal 頁面

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框架中,前端控制器起著最主要的作用.下面貼上代碼

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. version="3.0">
  6. <context-param> <!--全局範圍內環境參數初始化-->
  7. <param-name>contextConfigLocation</param-name> <!--參數名稱-->
  8. <param-value>classpath:spring-mybatis.xml</param-value> <!--參數取值-->
  9. </context-param>
  10. <!--以下配置的加載順序:先 ServletContext >> context-param >> listener >> filter >> servlet >> spring-->
  11. <!---------------------------------------------------過濾器配置------------------------------------------------------>
  12. <!--例:編碼過濾器-->
  13. <filter> <!-- 用來聲明filter的相關設定,過濾器可以截取和修改一個Servlet或JSP頁面的請求或從一個Servlet或JSP頁面發出的響應-->
  14. <filter-name>encodingFilter</filter-name> <!--指定filter的名字-->
  15. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--定義filter的類的名稱-->
  16. <async-supported>true</async-supported> <!--設置是否啟用異步支持-->
  17. <init-param><!--用來定義參數,若在Servlet可以使用下列方法來獲得:String param_name=getServletContext().getInitParamter("param-name裏面的值");-->
  18. <param-name>encoding</param-name> <!--參數名稱-->
  19. <param-value>UTF-8</param-value> <!--參數值-->
  20. </init-param>
  21. </filter>
  22. <filter-mapping><!--用來定義filter所對應的URL-->
  23. <filter-name>encodingFilter</filter-name> <!--指定對應filter的名字-->
  24. <url-pattern>/*</url-pattern> <!--指定filter所對應的URL-->
  25. </filter-mapping>
  26. <!---------------------------------------------------監聽器配置------------------------------------------------------>
  27. <!--例:spring監聽器-->
  28. <listener> <!--用來設定Listener接口-->
  29. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!--定義Listener的類名稱-->
  30. </listener>
  31. <!-- 防止Spring內存溢出監聽器 -->
  32. <listener>
  33. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  34. </listener>
  35. <!---------------------------------------------------servlet配置------------------------------------------------------>
  36. <servlet> <!--用來聲明一個servlet的數據 -->
  37. <servlet-name>SpringMVC</servlet-name> <!--指定servlet的名稱-->
  38. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--指定servlet的類名稱,這裏配置了前端控制器-->
  39. <init-param><!--用來定義參數,可有多個init-param。在servlet類中通過getInitParamenter(String name)方法訪問初始化參數 -->
  40. <param-name>contextConfigLocation</param-name> <!--參數名稱-->
  41. <param-value>classpath:spring-mvc.xml</param-value> <!--參數值-->
  42. </init-param>
  43. <load-on-startup>1</load-on-startup><!--當值為正數或零時:Servlet容器先加載數值小的servlet,再依次加載其他數值大的servlet.-->
  44. <async-supported>true</async-supported><!--設置是否啟用異步支持-->
  45. </servlet>
  46. <servlet-mapping><!--用來定義servlet所對應的URL-->
  47. <servlet-name>SpringMVC</servlet-name> <!--指定servlet的名稱-->
  48. <url-pattern>/</url-pattern> <!--指定servlet所對應的URL-->
  49. </servlet-mapping>
  50. <!-----------------------------------------------會話超時配置(單位為分鐘)------------------------------------------------->
  51. <session-config><!--如果某個會話在一定時間未被訪問,則服務器可以扔掉以節約內存-->
  52. <session-timeout>120</session-timeout>
  53. </session-config>
  54. <!---------------------------------------------------MIME類型配置 ------------------------------------------------------>
  55. <mime-mapping><!--設定某種擴展名的文件用一種應用程序來打開的方式類型,當該擴展名文件被訪問的時候,瀏覽器會自動使用指定應用程序來打開-->
  56. <extension>*.ppt</extension> <!--擴展名名稱-->
  57. <mime-type>application/mspowerpoint</mime-type> <!--MIME格式-->
  58. </mime-mapping>
  59. <!---------------------------------------------------歡迎頁面配置 ------------------------------------------------------>
  60. <welcome-file-list><!--定義首頁列單.-->
  61. <welcome-file>/index.jsp</welcome-file> <!--用來指定首頁文件名稱.我們可以用<welcome-file>指定幾個首頁,而服務器會依照設定的順序來找首頁.-->
  62. <welcome-file>/index.html</welcome-file>
  63. </welcome-file-list>
  64. <!---------------------------------------------------配置錯誤頁面------------------------------------------------------>
  65. <error-page> <!--將錯誤代碼(Error Code)或異常(Exception)的種類對應到web應用資源路徑.-->
  66. <error-code>404</error-code> <!--HTTP Error code,例如: 404、403-->
  67. <location>error.html</location> <!--用來設置發生錯誤或異常時要顯示的頁面-->
  68. </error-page>
  69. <error-page>
  70. <exception-type>java.lang.Exception</exception-type><!--設置可能會發生的java異常類型,例如:java.lang.Exception-->
  71. <location>ExceptionError.html</location> <!--用來設置發生錯誤或異常時要顯示的頁面-->
  72. </error-page>
  73. </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(適配器)[email protected]

context:component-scan 對指定的包進行掃描,實現註釋驅動Bean定義,[email protected]r標識的類的bean的註入和使用。

[html] view plain copy
  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"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
  14. http://www.springframework.org/schema/context
  15. http://www.springframework.org/schema/context/spring-context-4.1.xsd
  16. http://www.springframework.org/schema/mvc
  17. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  18. <!-- 1、配置映射器與適配器 -->
  19. <mvc:annotation-driven></mvc:annotation-driven>
  20. <!-- 2、視圖解析器 -->
  21. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  22. <span style="white-space:pre"> </span><!-- 前綴和後綴 -->
  23. <property name="prefix" value="/"/>
  24. <property name="suffix" value=".jsp"/>
  25. </bean>
  26. <!-- 3、自動掃描該包,[email protected] -->
  27. <context:component-scan base-package="com.rhzh.controller"/>
  28. </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 註解事務切面

[html] view plain copy
  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"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  9. http://www.springframework.org/schema/tx
  10. http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
  13. http://www.springframework.org/schema/context
  14. http://www.springframework.org/schema/context/spring-context-4.1.xsd">
  15. <!--1 自動掃描 將標註Spring註解的類自動轉化Bean-->
  16. <context:component-scan base-package="com.rhzh" />
  17. <!--2 加載數據資源屬性文件 -->
  18. <bean id="propertyConfigurer"
  19. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  20. <property name="location" value="classpath:jdbc.properties" />
  21. </bean>
  22. <span style="white-space:pre"><!-- 3 配置數據源 --></span>
  23. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  24. destroy-method="close">
  25. <property name="driverClassName" value="${driver}" />
  26. <property name="url" value="${url}" />
  27. <property name="username" value="${username}" />
  28. <property name="password" value="${password}" />
  29. <!-- 初始化連接大小 -->
  30. <property name="initialSize" value="${initialSize}"></property>
  31. <!-- 連接池最大數量 -->
  32. <property name="maxActive" value="${maxActive}"></property>
  33. <!-- 連接池最大空閑 -->
  34. <property name="maxIdle" value="${maxIdle}"></property>
  35. <!-- 連接池最小空閑 -->
  36. <property name="minIdle" value="${minIdle}"></property>
  37. <!-- 獲取連接最大等待時間 -->
  38. <property name="maxWait" value="${maxWait}"></property>
  39. </bean>
  40. <!-- 4 配置sessionfactory -->
  41. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  42. <property name="dataSource" ref="dataSource" />
  43. <!-- 自動掃描mapping.xml文件 -->
  44. <property name="mapperLocations" value="classpath:com/rhzh/mapping/*.xml"></property>
  45. </bean>
  46. <!-- 5 裝配dao接口 -->
  47. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  48. <property name="basePackage" value="com.rhzh.dao" /> <!-- DAO接口所在包名,Spring會自動查找其下的類 -->
  49. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  50. </bean>
  51. <!-- 6、聲明式事務管理 -->
  52. <bean id="transactionManager"
  53. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  54. <property name="dataSource" ref="dataSource" />
  55. </bean>
  56. <!-- 7、註解事務切面 --><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"> <tx:annotation-driven transaction-manager="transactionManager"/>
[html] view plain copy
  1. </beans>


下面是需要引入的兩個資源屬性文件:

jdbc.properties

[html] view plain copy
  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://127.0.0.1:3306/ecdatabase?characterEncoding=utf-8
  3. username=root
  4. password=admin
  5. #定義初始連接數
  6. initialSize=0
  7. #定義最大連接數
  8. maxActive=20
  9. #定義最大空閑
  10. maxIdle=20
  11. #定義最小空閑
  12. minIdle=1
  13. #定義最長等待時間
  14. maxWait=60000

log4j.properties

[html] view plain copy
  1. #定義LOG輸出級別
  2. log4j.rootLogger=INFO,Console,File
  3. #定義日誌輸出目的地為控制臺
  4. log4j.appender.Console=org.apache.log4j.ConsoleAppender
  5. log4j.appender.Console.Target=System.out
  6. #可以靈活地指定日誌輸出格式,下面一行是指定具體的格式
  7. log4j.appender.Console.layout = org.apache.log4j.PatternLayout
  8. log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
  9. #文件大小到達指定尺寸的時候產生一個新的文件
  10. log4j.appender.File = org.apache.log4j.RollingFileAppender
  11. #指定輸出目錄
  12. log4j.appender.File.File = logs/ssm.log
  13. #定義文件最大大小
  14. log4j.appender.File.MaxFileSize = 10MB
  15. # 輸出所以日誌,如果換成DEBUG表示輸出DEBUG以上級別日誌
  16. log4j.appender.File.Threshold = ALL
  17. log4j.appender.File.layout = org.apache.log4j.PatternLayout
  18. log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

以上僅僅是對於框架中的配置文件進行解釋說明,整合框架並不是能夠運行就OK了,了解其中的原理以便於以後發現問題和解決問題,不要急於求成,一步一步走踏實了,你會發現你比別人走的更快!!!

SSM:spring+springmvc+mybatis框架中的XML配置文件功能詳細解釋