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.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">


	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql:///test?useUnicode=true&amp;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" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation">
			<value>classpath:sqlMap-config.xml</value>
		</property>
		<property name="dataSource" ref="dataSource" />
	</bean>

	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" 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" class="com.ksfzhaohui.load.LogService"></bean>

	<!-- Spring AOP config -->
	<aop:config>
		<!-- 配置哪些类的方法需要进行事务管理 --> 
		<aop:pointcut id="newServicesPointcut"
			expression="execution(* com.ksfzhaohui.service.impl.*.*(..))" />
		<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());
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值