<!--事务注解-->
<tx:annotation-driven />
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--Spring AOP事务配置-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" rollback-for="Exception"/>
<tx:method name="insert*" rollback-for="Exception"/>
<tx:method name="update*" rollback-for="Exception"/>
<tx:method name="*" read-only="true" propagation="NOT_SUPPORTED" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceImplMethod" expression="execution(* com.xx.xx.xx.service.impl..*.*(..))"/>
<aop:advisor pointcut-ref="serviceImplMethod" advice-ref="txAdvice"/>
<!-- <aop:pointcut id="daoMethod"
expression="execution(* com.xx.xx.dao.impl..*.*(..))"/>
<aop:advisor pointcut-ref="daoMethod" advice-ref="txAdvice"/> -->
</aop:config>
需要注意的地方:
(1) advice(建议)的命名:由于每个模块都会有自己的Advice,所以在命名上需要作出规范,初步的构想就是模块名+Advice(只是一种命名规范)。
(2) tx:attribute标签所配置的是作为事务的方法的命名类型。
如<tx:method name="save*" propagation="REQUIRED"/>
其中*为通配符,即代表以save为开头的所有方法,即表示符合此命名规则的方法作为一个事务。
propagation="REQUIRED"代表支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
(3) aop:pointcut标签配置参与事务的类,由于是在Service中进行数据库业务操作,配的应该是包含那些作为事务的方法的Service类。
首先应该特别注意的是id的命名,同样由于每个模块都有自己事务切面,所以我觉得初步的命名规则因为 all+模块名+ServiceMethod。而且每个模块之间不同之处还在于以下一句:
expression="execution(* com.test.testAda.test.model.service.*.*(..))"
其中第一个*代表返回值,第二*代表service下子包,第三个*代表方法名,“(..)”代表方法参数。
(4) aop:advisor标签就是把上面我们所配置的事务管理两部分属性整合起来作为整个事务管理。
/**
* @Description: annotation实现AOP,session超时和日志控制
*/
@Aspect
@Component
public class SessionAop {
/**
* 定义PointCut,无返回值,只是一个标识。 括号内容是表达式,标识哪些对象的哪些方法
* @Pointcut("execution(* add*(..)) || execution(* del*(..))")
*/
@Pointcut("execution(public* com.xx.xx.xx.controller.*.*(..))")
private void servletCall() {
}
/**
* 方法执行前处理
*
* @param jp
*/
@Before("servletCall()")
public void sessionTimeOutExceptionAspect(JoinPoint jp) {
User user = null;
for (Object param : jp.getArgs()) {
if (param instanceof HttpServletRequest) {
user = (User) ((HttpServletRequest) param).getSession().getAttribute(ConstantsCtr.SESSION_USER);
}
}
if (user == null) {
throw new RuntimeException("操作超时");
}
System.out.println("servletCallBefore" + user.getId());//test
}
}
<tx:annotation-driven />
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--Spring AOP事务配置-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" rollback-for="Exception"/>
<tx:method name="insert*" rollback-for="Exception"/>
<tx:method name="update*" rollback-for="Exception"/>
<tx:method name="*" read-only="true" propagation="NOT_SUPPORTED" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceImplMethod" expression="execution(* com.xx.xx.xx.service.impl..*.*(..))"/>
<aop:advisor pointcut-ref="serviceImplMethod" advice-ref="txAdvice"/>
<!-- <aop:pointcut id="daoMethod"
expression="execution(* com.xx.xx.dao.impl..*.*(..))"/>
<aop:advisor pointcut-ref="daoMethod" advice-ref="txAdvice"/> -->
</aop:config>
需要注意的地方:
(1) advice(建议)的命名:由于每个模块都会有自己的Advice,所以在命名上需要作出规范,初步的构想就是模块名+Advice(只是一种命名规范)。
(2) tx:attribute标签所配置的是作为事务的方法的命名类型。
如<tx:method name="save*" propagation="REQUIRED"/>
其中*为通配符,即代表以save为开头的所有方法,即表示符合此命名规则的方法作为一个事务。
propagation="REQUIRED"代表支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
(3) aop:pointcut标签配置参与事务的类,由于是在Service中进行数据库业务操作,配的应该是包含那些作为事务的方法的Service类。
首先应该特别注意的是id的命名,同样由于每个模块都有自己事务切面,所以我觉得初步的命名规则因为 all+模块名+ServiceMethod。而且每个模块之间不同之处还在于以下一句:
expression="execution(* com.test.testAda.test.model.service.*.*(..))"
其中第一个*代表返回值,第二*代表service下子包,第三个*代表方法名,“(..)”代表方法参数。
(4) aop:advisor标签就是把上面我们所配置的事务管理两部分属性整合起来作为整个事务管理。
/**
* @Description: annotation实现AOP,session超时和日志控制
*/
@Aspect
@Component
public class SessionAop {
/**
* 定义PointCut,无返回值,只是一个标识。 括号内容是表达式,标识哪些对象的哪些方法
* @Pointcut("execution(* add*(..)) || execution(* del*(..))")
*/
@Pointcut("execution(public* com.xx.xx.xx.controller.*.*(..))")
private void servletCall() {
}
/**
* 方法执行前处理
*
* @param jp
*/
@Before("servletCall()")
public void sessionTimeOutExceptionAspect(JoinPoint jp) {
User user = null;
for (Object param : jp.getArgs()) {
if (param instanceof HttpServletRequest) {
user = (User) ((HttpServletRequest) param).getSession().getAttribute(ConstantsCtr.SESSION_USER);
}
}
if (user == null) {
throw new RuntimeException("操作超时");
}
System.out.println("servletCallBefore" + user.getId());//test
}
}