关于spring 事务 和 AOP 管理事务和打印日志问题
1. <tx:annotation-driven /> 就是支持事务注解的(@Transactional) 。
可以在server层总使用@Transactional,进行方法内的事务管理
配置
<!-- 事务注解-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:orcl</value>
</property>
<property name="username">
<value>xxx</value>
</property>
<property name="password">
<value>xxx</value>
</property>
<property name="maxActive">
<value>255</value>
</property>
<property name="maxIdle">
<value>2</value>
</property>
<property name="maxWait">
<value>120000</value>
</property>
</bean>
2. 在applicationContent.xml 引入 applicationContext-aop.xml
<?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"
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 ">
<!-- aop 统一处理类 -->
<bean id="serviceLogging" class="com.xxx.hrzcyp.aop.ServiceLogging"/>
<!-- aop 配置 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut id="serviceMethodLoggingPointcut" expression="execution(* com.xxx.service.impl.*.*(..))"/>
<!-- 定义 切面 order 的值越小,说明越先被执行 -->
<aop:aspect id="serviceMethodLoggingAspect" ref="serviceLogging" order="100">
<aop:before method="loggingMethodInvoked" pointcut-ref="serviceMethodLoggingPointcut"/>
<aop:after-throwing method="loggingServiceException" pointcut-ref="serviceMethodLoggingPointcut" throwing="throwable"/>
</aop:aspect>
</aop:config>
</beans>
3. 用于aop处理日志和异常的java
package com.xxx.aop;
import org.aspectj.lang.JoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* AOP 统一处理异常和返回值
*/
public class ServiceLogging {
private static Logger exceptionLogger = LoggerFactory.getLogger("ExceptionLogging");
private static Logger methodInvokeLogger = LoggerFactory.getLogger("MethodInvokeLogging");
/**
* 捕获Service层抛出的异常并做日志
*
* @param throwable 方法抛出的异常
*/
public void loggingServiceException(Throwable throwable) {
if (exceptionLogger.isErrorEnabled()) {
StringBuilder builder = new StringBuilder();
builder.append("cause:").append(throwable.getMessage());
builder.append("\n\tstackTrack:\n");
for (StackTraceElement stack : throwable.getStackTrace()) {
builder.append("\t\t");
builder.append(stack.toString());
builder.append("\n");
}
exceptionLogger.error(builder.toString());
}
}
/**
* 记录Service方法的调用
*
* @param joinPoint 切入点
*/
public void loggingMethodInvoked(JoinPoint joinPoint) {
if (methodInvokeLogger.isDebugEnabled()) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
StringBuilder builder = new StringBuilder();
builder.append("调用方法[AOP切入点]:").append(methodName);
builder.append("\t参数:");
for (Object arg : args) {
builder.append(arg.toString());
}
methodInvokeLogger.debug(builder.toString());
}
}
}
总结
以上工作做完后,只要有访问当 com.xxx.service.impl 目录下的所有方法。aop都会帮助我们答应访问的日志,如果加上 @Transactional 会将整个方法内的事务统一管理。有异常时也会通过aop抛出异常。