解决springboot的aop切面不起作用问题(失效的排查)
检查下springboot的启动类是否开启扫描
@SpringBootApplication
@ComponentScan(basePackages={"com.zhangpu.springboot"})
另外springboot默认开启的EnableAspectJAutoProxy为true
如果不放心可以增加:
@EnableAspectJAutoProxy(proxyTargetClass=true)
第二种可能:
没有导入相关的jar
org.aspectj aspectjrt org.aspectj aspectjweaver
补充知识:Springbootaop实现全局配置事务
1.springboot实现事务只需要在头上加上@Transactional注解
@Transactional默认只捕获RuntimeException.class
对Exception异常得需要@Transactional(rollbackFor={Exception.class})捕获回滚。
2.当项目特别大的时候,对所有的service都加上事务,显得非常麻烦。可以通过aop方式实现全局异常。
代码如下。
这样就能专注写业务逻辑然后注意一下方法名就行了,当然也可以用
source.addTransactionalMethod("*",txAttr_REQUIRED);匹配所有方法,但是并不是所有的方法都需要事务的。所有推荐匹配方法名的方法
packagecom.example.demo.aop;
importorg.aspectj.lang.annotation.Aspect;
importorg.springframework.aop.Advisor;
importorg.springframework.aop.aspectj.AspectJExpressionPointcut;
importorg.springframework.aop.support.DefaultPointcutAdvisor;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.transaction.PlatformTransactionManager;
importorg.springframework.transaction.TransactionDefinition;
importorg.springframework.transaction.interceptor.DefaultTransactionAttribute;
importorg.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
importorg.springframework.transaction.interceptor.TransactionInterceptor;
/**
*@program:Druid
*@description:
*@author:ChenYu
*@create:2018-09-2016:47
**/
@Aspect
@Configuration
publicclassTransactionAdviceConfig{
privatestaticfinalStringAOP_POINTCUT_EXPRESSION="execution(*com.example.demo.service.*.*(..))";
@Autowired
privatePlatformTransactionManagertransactionManager;
@Bean
publicTransactionInterceptortxAdvice(){
DefaultTransactionAttributetxAttr_REQUIRED=newDefaultTransactionAttribute();
txAttr_REQUIRED.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
DefaultTransactionAttributetxAttr_REQUIRED_READONLY=newDefaultTransactionAttribute();
txAttr_REQUIRED_READONLY.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
txAttr_REQUIRED_READONLY.setReadOnly(true);
NameMatchTransactionAttributeSourcesource=newNameMatchTransactionAttributeSource();
source.addTransactionalMethod("save*",txAttr_REQUIRED);
source.addTransactionalMethod("delete*",txAttr_REQUIRED);
source.addTransactionalMethod("update*",txAttr_REQUIRED);
source.addTransactionalMethod("exec*",txAttr_REQUIRED);
source.addTransactionalMethod("set*",txAttr_REQUIRED);
source.addTransactionalMethod("get*",txAttr_REQUIRED_READONLY);
source.addTransactionalMethod("query*",txAttr_REQUIRED_READONLY);
source.addTransactionalMethod("find*",txAttr_REQUIRED_READONLY);
source.addTransactionalMethod("list*",txAttr_REQUIRED_READONLY);
source.addTransactionalMethod("count*",txAttr_REQUIRED_READONLY);
source.addTransactionalMethod("is*",txAttr_REQUIRED_READONLY);
returnnewTransactionInterceptor(transactionManager,source);
}
@Bean
publicAdvisortxAdviceAdvisor(){
AspectJExpressionPointcutpointcut=newAspectJExpressionPointcut();
pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
returnnewDefaultPointcutAdvisor(pointcut,txAdvice());
}
}
以上这篇解决springboot的aop切面不起作用问题(失效的排查)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。