spring MVC 資料

一、web.xml
org.springframework.web.filter.CharacterEncodingFilter;
配置字符編碼,配置示例:
<filter>
<filter-name>encode</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
上下文
org.springframework.web.servlet.DispatcherServletjavascript

<servlet>
<servlet-name>mcc-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring-servlet.xml</param-value><!-- 初始化上下文,能夠不指定,使用默認配置文件 (servlet名稱-servlet) -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mcc-web</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>html

<!-- 其餘spring配置文件 或者在以上contextConfigLocation配置多個文件經過*匹配-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/spring-application.xml,
/WEB-INF/spring/spring-servlet.xml,
/WEB-INF/spring/spring-task.xml
</param-value>
</context-param>java

其餘配置
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>web

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name><!-- log4jRefreshInterval爲6000表示 開一條watchdog線程每6秒掃描一下配置文件的變化;動態刷新日誌級別 -->
<param-value>60000</param-value>
</context-param>spring

<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>sql

 




二、spring-servlet.xml數據庫

<!-- 隱式地向 Spring 容器註冊
至關於註冊如下bean
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor 使用@Autowired註解
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor 使用@ Resource 、@ PostConstruct、@ PreDestroy等註解
org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor 使用@PersistenceContext註解
org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor @Required的註解*/
-->
<context:annotation-config />express

 

視圖過濾器:
org.springframework.web.servlet.view.InternalResourceViewResolver
示例:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/web/" />
<property name="suffix" value=".jsp" />
<property name="viewClass">
<value>org.springframework.web.servlet.view.InternalResourceView</value>
</property>
</bean>json

上傳文件
org.springframework.web.multipart.commons.CommonsMultipartResolver
示例:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<!-- 50M -->
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="2547456500" />
</bean>mybatis

---------
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="cacheSeconds" value="0" />
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>

<!-- 異常處理,將AnnotationMethodHandlerAdapter一樣規則來輸出結果-->
<bean id="handlerExceptionResolver" class="com.microcloud.mcc.annotation.HandlerMethodExceptionResolver">
<!-- 沒有被#ResponseBody標記的Controller方法將返回錯誤頁面 -->
<property name="defaultErrorView" value="error.jsp"/>
<!-- 標有#ResponseBody註解的Controller方法,將使用此輸出JSON-->
<property name="messageConverters" ref="mappingJacksonHttpMessageConverter"/>
</bean>

<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
<value>application/javascript</value>
<value>text/json</value>
<value>text/javascript</value>
<value>application/xml</value>
<value>text/plain; charset=UTF-8</value>
</list>
</property>
</bean>

攔截
<!--**************interceptors **************************-->
<bean id="accessInterceptor" class="com.microcloud.mcc.interceptor.AccessInterceptor">
<property name="loginUrl" value="/login.jsp"/>
<property name="errorUrl" value="/error.jsp"/>
<property name="excludeUrls">
<list>
<value>/login.htm</value>
<value>/index.htm</value>
<value>/logout.htm</value>
<value>/three.htm</value>
<value>/toNoticelist.htm</value>
<value>/getNotice.htm</value>
<value>/queryNotice.htm</value>
</list>
</property>
</bean>

<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/control/**" />
<ref bean="accessInterceptor"/>
</mvc:interceptor>
<mvc:interceptors>
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
org.aspectj.lang.annotation.Aspect
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
org.springframework.web.servlet.HandlerInterceptor;

org.springframework.scheduling.timer.ScheduledTimerTask
org.springframework.scheduling.timer.TimerFactoryBean


控制器配置
<context:component-scan base-package="com.microcloud.mcc.controller">
<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect" />
</context:component-scan>


其餘配置

<!-- 使用Spring組件掃描的方式來實現自動注入bean -->
<context:component-scan base-package="com.microcloud.mcc" />
<!-- 隱式地向 Spring 容器註冊 -->
<context:annotation-config />
<!-- 開啓mvc註解,日期格式化等 -->
<mvc:annotation-driven/>
<!-- 定時器開關 開始 -->
<task:annotation-driven />
<!-- 定時器開關 結束 -->

<!-- 標註類型 的事務配置 -->
<tx:annotation-driven />

<!-- 讀取資源文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/jdbc.properties" />
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${driverClassName}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${username}"/>
<property name="password" value="${password}"/>
<property name="maxPoolSize" value="100"/><!--鏈接池中保留的最大鏈接數 -->
<property name="minPoolSize" value="2"/><!--鏈接池中保留的最小鏈接數 -->
<property name="initialPoolSize" value="10"/>
<property name="maxIdleTime" value="1800"/><!--最大空閒時間,1800秒內未使用則鏈接被丟棄。若爲0則永不丟棄-->
<property name="acquireIncrement" value="3" /><!--當鏈接池中的鏈接耗盡的時候c3p0一次同時獲取的鏈接數-->
<property name="maxStatements" value="1000" />
<property name="acquireRetryAttempts" value="10" /><!-- 定義在從數據庫獲取新鏈接失敗後重復嘗試的次數-->
<property name="idleConnectionTestPeriod" value="60" /><!--每60秒檢查全部鏈接池中的空閒鏈接-->
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>

 

mybatis 配置

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="/WEB-INF/mybatis/mybatis.xml" />
<property name="plugins">
<array>
<ref bean="pagePlugin" />
</array>
</property>
<property name="mapperLocations">
<list>
<!-- 自動匹配Mapper映射文件 -->
<!-- <value>classpath:com/microcloud/mcc/entity/*-mapper.xml</value> -->
<value>classpath:sql/*.xml</value>
</list>
</property>
<property name="typeAliasesPackage" value="com.microcloud.mcc.vo,com.microcloud.mcc.entity" />
</bean>
<!-- 經過掃描的模式,掃描目錄在com.microcloud.mcc.dao目錄下,全部的mapper都繼承SqlMapper接口的接口, 這樣一個bean就能夠了 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.microcloud.mcc.dao"/>
<property name="markerInterface" value="com.microcloud.mcc.sqlmap.SqlMapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

 

事務配置

切點 pointcut

下面給出一些常見切入點表達式的例子:
任意公共方法的執行:
execution(public * *(..))
任何一個以「set」開始的方法的執行:
execution(* set*(..))
AccountService 接口的任意方法的執行:
execution(* com.xyz.service.AccountService.*(..))
定義在service包裏的任意方法的執行:
execution(* com.xyz.service.*.*(..))
定義在service包或者子包裏的任意類的任意方法的執行:
execution(* com.xyz.service..*.*(..))

事務隔離級別
1、Propagation(事務的傳播屬性), key屬性肯定代理應該給哪一個方法增長事務行爲。這樣的屬性最重要的部份是傳播行爲。有如下選項可供使用:
Enumerated Values :
- REQUIRED --支持當前事務,若是當前沒有事務,就新建一個事務。這是最多見的選擇。
- SUPPORTS --支持當前事務,若是當前沒有事務,就以非事務方式執行。
- MANDATORY --支持當前事務,若是當前沒有事務,就拋出異常。
- REQUIRES_NEW --新建事務,若是當前存在事務,把當前事務掛起。
- NOT_SUPPORTED --以非事務方式執行操做,若是當前存在事務,就把當前事務掛起。
- NEVER --以非事務方式執行,若是當前存在事務,則拋出異常。
- NESTED


Isolation (事務隔離等級): 屬性一共支持五種事務設置,具體介紹以下:
Enumerated Values :
- DEFAULT 使用數據庫設置的隔離級別 ( 默認 ) ,由 DBA 默認的設置來決定隔離級別 .
- READ_UNCOMMITTED 會出現髒讀、不可重複讀、幻讀 ( 隔離級別最低,併發性能高 )
- READ_COMMITTED 會出現不可重複讀、幻讀問題(鎖定正在讀取的行)
- REPEATABLE_READ 會出幻讀(鎖定所讀取的全部行)
- SERIALIZABLE 保證全部的狀況不會發生(鎖表)

<!-- 事務配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<aop:config> <!-- 定義在service包或者子包裏的任意類的public方法的執行:-->
<aop:pointcut id="pointcut" expression="execution(public * com.microcloud.mcc.service.impl..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="import*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>

<aop:aspectj-autoproxy proxy-target-class="true" />


資源文件配置
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>resources.localization_zh_CN</value>
</list>
</property>
<property name="useCodeAsDefaultMessage" value="true" />
</bean>

<!--定義定時任務類 extends TimerTask http://sunny.blog.51cto.com/182601/32366 --> <bean id="demoTask" class="com.DemoTask"/> <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <!--這裏定義定時任務的對象的位置--> <property name="timerTask"> <ref bean="demoTask"/> </property> <!--這裏定義每六秒鐘程序執行一次--> <property name="period"> <value>6000</value> </property> <!--這裏定義程序啓動兩秒鐘後開始執行--> <property name="delay"> <value>2000</value> </property> </bean> <bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref bean="scheduledTimerTask"/> </list> </property> </bean>

相關文章
相關標籤/搜索