1、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.DispatcherServlet

<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>

<!-- 其他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>

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

<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>

<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>

 




2、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 />

 

视图过滤器:
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>

上传文件
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>

---------
<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..*.*(..))

事务隔离级别
一、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>

posted on 2015-04-10 16:58  Hux、Diy  阅读(280)  评论(0编辑  收藏  举报