SSM:spring+springmvc+mybatis框架中的XML配置文件功能詳細解釋
SSM:spring+springmvc+mybatis框架中的XML配置文件功能詳細解釋
2016-04-14 23:40 13030人閱讀 評論(2) 收藏 舉報 分類:這幾天一直在整合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
- <?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(適配器)[email protected]
context:component-scan 對指定的包進行掃描,實現註釋驅動Bean定義,[email protected]r標識的類的bean的註入和使用。
[html] view plain copy
- <?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、自動掃描該包,[email protected] -->
- <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 註解事務切面
[html] view plain copy
- <?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"/>
- </beans>
下面是需要引入的兩個資源屬性文件:
jdbc.properties
[html] view plain copy
- 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
[html] view plain copy
- #定義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了,了解其中的原理以便於以後發現問題和解決問題,不要急於求成,一步一步走踏實了,你會發現你比別人走的更快!!!
SSM:spring+springmvc+mybatis框架中的XML配置文件功能詳細解釋