ssm框架中的配置檔案
spring+springmvc+mybatis框架中用到了三個XML配置檔案:web.xml,spring-mvc.xml,spring-mybatis.xml
專案中還會用到兩個資源屬性檔案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">
<!-- 以下配置的載入順序:ServletContext >> context-param >> listener >> filter >>
servlet >> spring -->
<!-- ps:建立:監聽器 >> 過濾器 >>Servlet -->
<!--******************************************************全域性範圍內環境引數配置****************************************************** -->
<!-- 全域性範圍內環境引數初始化 -->
<context-param>
<!-- 引數名稱 -->
<param-name >contextConfigLocation</param-name>
<!-- 引數值 -->
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!--******************************************************監聽器配置****************************************************** -->
<!-- e.g.spring監聽器 -->
<!-- 用來設定LIstener介面 -->
<listener>
<!--定義Listener的類名稱 -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止spring記憶體溢位的監聽器 -->
<listener>
<!--定義Listener的類名稱 -->
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!--******************************************************過濾器配置****************************************************** -->
<!-- e.g.編碼過濾器 -->
<!-- 用來宣告filter的相關設定,過濾器可以擷取和修改一個servlet或jsp頁面的請求或從一個servlet或jsp頁面發出的響應 -->
<filter>
<!-- 指定filter的名字 -->
<filter-name>encodingFilter</filter-name>
<!-- 定義filter的類的名稱 -->
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 設定是否啟用非同步支援 -->
<async-supported>true</async-supported>
<!-- 用來定義引數,若在servlet可以使用下列方法來獲取:String param_name = getServletContext().getInitParamter("param_name裡面的值"); -->
<init-param>
<!-- 引數名稱 -->
<param-name>encoding</param-name>
<!-- 引數值 -->
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- 用來定義filter所對應的URL -->
<filter-mapping>
<!-- 指定對應filter的名字 -->
<filter-name>encodingFilter</filter-name>
<!-- 指定filter所對應的URL 要攔截的URL -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--******************************************************servlet配置****************************************************** -->
<!-- e.g.spring 前端控制器 -->
<!-- 用來宣告一個servlet的資料 -->
<servlet>
<!-- 指定servlet的名稱 -->
<servlet-name>SpringMVC</servlet-name>
<!-- 指定servlet的類的名稱,這裡配置了前端控制器 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 用來定義引數,可有多個init-param, 在servlet類中通過getInitParamenter(String name)方法訪問初始化引數 -->
<init-param>
<!-- 引數名稱 -->
<param-name>contextConfigLocation</param-name>
<!-- 引數值 -->
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 當值為正數或零時:Servlet容器先載入數值小的servlet,再依次載入其他數值大的servlet -->
<load-on-startup>1</load-on-startup>
<!-- 設定是否啟用非同步支援 -->
<async-supported>true</async-supported>
<!-- 檔案上傳配置 -->
<multipart-config>
<!-- 檔案路徑 -->
<location></location>
<!-- 檔案的最大大小,單位為位元組 -->
<max-file-size>5242880</max-file-size>
<!-- 請求的最大大小,單位為位元組 -->
<max-request-size>10485760</max-request-size>
<!--檔案大小閾值,當大於這個閾值時將寫入到磁碟,否則在記憶體中。預設值為0 -->
<file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet>
<!-- 用來定義servlet所對應的URL -->
<servlet-mapping>
<!-- 指定servlet的名稱 -->
<servlet-name>SpringMVC</servlet-name>
<!-- 指定servlet所對應的URL -->
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!--******************************************************會話超時配置(單位為分鐘)****************************************************** -->
<!-- 如果某個會話在一定的時間未被訪問,則伺服器可以扔掉以節約資源 -->
<session-config>
<session-timeout>120</session-timeout>
</session-config>
<!--******************************************************MIME型別配置****************************************************** -->
<!-- 設定某種副檔名的檔案用一種應用程式來開啟的方式型別,當該副檔名檔案被訪問的時候,瀏覽器會自動使用指定應用程式來開啟 -->
<mime-mapping>
<!-- 副檔名名稱 -->
<extension>*.ppt</extension>
<!-- MIME格式 -->
<mime-type>application/mspowerpoint</mime-type>
</mime-mapping>
<!--******************************************************歡迎頁面配置****************************************************** -->
<!-- 定義首頁列單 -->
<welcome-file-list>
<!-- 用來指定首頁檔名稱可以用<welcome-file>指定幾個首頁,而伺服器會依照順序來找首頁 -->
<welcome-file>/index.jsp</welcome-file>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
<!--******************************************************錯誤頁面配置****************************************************** -->
<!-- 將錯誤程式碼(Error Code)或異常(Exception)的種類對應到web應用資源路徑 -->
<error-page>
<!-- HTTP Error code, e.g.404 500 -->
<error-code>404</error-code>
<!-- 用來設定發生錯誤或者異常時要顯示的頁面 -->
<location>/error.html</location>
</error-page>
<error-page>
<!-- 設定可能會發生異常的java異常型別,例如:java.lang.Exception -->
<exception-type>java.lang.Exception</exception-type>
<!-- 用來設定發生錯誤或者異常時要顯示的頁面 -->
<location>/ExceptionError.html</location>
</error-page>
</web-app>
二,spring-mvc.xml
需要實現基本功能的配置
- mvc :annotation-driven
- context:component-scan
- 配置檢視解析器
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--******************************************************對映器與介面卡配置****************************************************** -->
<mvc:annotation-driven></mvc:annotation-driven>
<!--******************************************************試圖解析器配置****************************************************** -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 字首 -->
<property name="prefix" value="/WEB-INF/"></property>
<!-- 字尾 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!--******************************************************試圖解析器配置****************************************************** -->
<!-- 自動掃描包,使SpringMVC認為包下用了@controller註解的類是控制器 -->
<context:component-scan base-package=""></context:component-scan>
<!--******************************************************注入事務****************************************************** -->
<!-- 配置事務 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!--******************************************************json配置****************************************************** -->
<!-- 配置使用json -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonHttpMessageConverter" />
</list>
</property>
</bean>
<!-- 配置使用json檢視解析 -->
<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!--******************************************************攔截器配置****************************************************** -->
<!-- 攔截器可以實現多個 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 注意路徑是** 參考Ant的規範 -->
<mvc:mapping path="/**" />
<!-- 不攔截的路徑 -->
<mvc:exclude-mapping path="" />
<!-- 攔截器具體實現類 -->
<bean class=""></bean>
</mvc:interceptor>
</mvc:interceptors>
</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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 切面配置 -->
<aop:aspectj-autoproxy />
<!-- c3p0配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}" />
<property name="jdbcUrl" value="${url}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
<!-- 關鍵配置 -->
<!--初始化時獲取三個連線,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--連線池中保留的最小連線數。Default: 2 -->
<property name="minPoolSize" value="2"></property>
<!--連線池中保留的最大連線數。Default: 15 -->
<property name="maxPoolSize" value="15"></property>
<!--當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 效能配置 -->
<!-- 控制資料來源內載入的PreparedStatements數量。如果maxStatements與maxStatementsPerConnection均為0,則快取被關閉。Default:
0 -->
<property name="maxStatements" value="8"></property>
<!-- maxStatementsPerConnection定義了連線池內單個連線所擁有的最大快取statements數。Default:
0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空閒時間,1800秒內未使用則連線被丟棄。若為0則永不丟棄。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
<!-- 裝配dao介面 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper" />
</bean>
<!-- 宣告事務管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
四,資源屬性檔案
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
以上是常用的配置