改造后的SpringBoot基于Java代码:
- @Aspect
- @Configuration
- public class TransactionAdviceConfig {
- /**
- * 指定切入点
- */
- private static final String AOP_POINTCUT_EXPRESSION = "execution(public * com.creditease.permission.service.impl.*Impl.*(..))";
-
- @Resource
- DruidDataSource dataSource;
-
- /**
- * 指定处理事务的PlatformTransactionManager
- * @return
- */
- @Bean
- public DataSourceTransactionManager transactionManager() {
-
- return new DataSourceTransactionManager(dataSource);
-
- }
-
- /**
- * 指定切入点处理逻辑,执行事务
- * @return
- */
- @Bean
- public TransactionInterceptor txAdvice() {
-
- DefaultTransactionAttribute txAttrRequired = new DefaultTransactionAttribute();
- txAttrRequired.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
-
- DefaultTransactionAttribute txAttrRequiredReadonly = new DefaultTransactionAttribute();
- txAttrRequiredReadonly.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
- txAttrRequiredReadonly.setReadOnly(true);
-
- NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
- source.addTransactionalMethod("query*", txAttrRequiredReadonly);
- source.addTransactionalMethod("find*", txAttrRequiredReadonly);
- source.addTransactionalMethod("save*", txAttrRequired);
- source.addTransactionalMethod("delete*", txAttrRequired);
- source.addTransactionalMethod("add*", txAttrRequired);
- source.addTransactionalMethod("modify*", txAttrRequired);
- return new TransactionInterceptor(transactionManager(), source);
- }
-
- /**
- * Advisor组装配置,将Advice的代码逻辑注入到Pointcut位置
- * @return
- */
- @Bean
- public Advisor txAdviceAdvisor() {
-
- AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
- pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
- return new DefaultPointcutAdvisor(pointcut, txAdvice());
- }
5)全局异常处理
(编辑:ASP站长网)
|