读书人

Spring的那些配备(事务和日志)

发布时间: 2012-09-23 10:28:11 作者: rapoo

Spring的那些配置(事务和日志)

Spring整合Ibatis典型的配置文件

<?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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><bean id="dataSource" value="com.mysql.jdbc.Driver" /><property name="url"value="jdbc:mysql:///test?useUnicode=true&characterEncoding=utf-8" /><property name="username" value="root" /><property name="password" value="root" /><property name="maxActive" value="100" /><property name="maxIdle" value="30" /><property name="maxWait" value="10000" /><property name="defaultAutoCommit" value="false" /> <!--设置自动提交 --></bean><bean id="sqlMapClient" ref="dataSource" /></bean><bean id="transactionManager"ref="dataSource" /></bean><!-- 事务通知      要注明rollback-for类型,并不是所有的异常都回滚的 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable"/><tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable"/><tx:method name="mod*" propagation="REQUIRED" rollback-for="Throwable"/><!--除了上面标识的方法,其他方法全是只读方法--><tx:method name="*" read-only="true" /></tx:attributes></tx:advice><!-- 日志Bean --><bean id="logService" /><aop:advisor advice-ref="txAdvice" pointcut-ref="newServicesPointcut" /><!-- 日志管理 --><aop:aspect id="myAspect" ref="logService"><aop:pointcut expression="execution(* com.ksfzhaohui.service.impl.*.*(..))"id="logPointCut" /><aop:before method="before" pointcut-ref="logPointCut" /><aop:after method="after" pointcut-ref="logPointCut" /></aop:aspect></aop:config></beans>



1.Spring的事务配置
Spring使用 <tx:advice>和 <aop:config> 用来配置事务,具体如何配置你可以参考Spring文档
关于这里的expression="execution(* com.ksfzhaohui.service.impl.*.*(..))"分析
第一个*,表示任何返回值类型
第二个*,表示com.ksfzhaohui.service.impl包下的任何类
第三个*,表示com.ksfzhaohui.service.impl包下的任何类下的任意方法
最后的(..),表示方法可以有0个或多个参数

<tx:advice>中的相关属性

属性是否需要默认值描述name是与事务属性关联的方法名。propagation不是REQUIRED事务传播行为isolation不是DEFAULT事务隔离级别timeout不是-1事务超时的时间(以秒为单位)read-only不是false事务是否只读rollback-for不是将被触发进行回滚的 Exception(s)以逗号分开。?no-rollback-for不是不被触发进行回滚的 Exception(s)以逗号分开。?

?

?

2.日志配置

<aop:before>,<aop:after>分别表示在方法执行之前和之后要执行的内容

? ? 典型案例

public class LogService {static final Log log = LogFactory.getLog(LogService.class);public void before(JoinPoint joinPoint) {StringBuffer sb=new StringBuffer();sb.append("logInfo--调用类" + joinPoint.getTarget().getClass().getName()+"--方法" + joinPoint.getSignature().getName());Object args[] = joinPoint.getArgs();for (int i = 0; i < args.length; i++) {sb.append("--参数" + i + ":" + args[i]);}log.info(sb);}public void after(JoinPoint joinPoint) {log.info("结束方法:" + joinPoint.getSignature().getName());}}
?

读书人网 >编程

热点推荐