小编这次要给大家分享的是springboot的aop切面不起作用怎么办,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。
检查下springboot的启动类是否开启扫描
@SpringBootApplication
引用>
@ComponentScan (basePackages={“com.zhangpu.springboot"})另外springboot默认开启的EnableAspectJAutoProxy为真正的
如果不放心可以增加:
@EnableAspectJAutoProxy (proxyTargetClass=true)
引用><强>第二种可能:强>
没有导入相关的jar
& lt; dependency> & lt; groupId> org.aspectj & lt; artifactId> aspectjrt & lt;/dependency> & lt; dependency> & lt; groupId> org.aspectj & lt; artifactId> aspectjweaver & lt;/dependency><强> Springboot aop实现全局配置事务强>
1。springboot实现事务只需要在头上加上@ transactional注解
@ transactional默认只捕获RuntimeException。类
对异常异常得需要@ transactional (rollbackFor={Exception.class})捕获回滚。
2。当项目特别大的时候,对所有的服务都加上事务,显得非常麻烦。可以通过aop方式实现全局异常。
代码如下。
这样就能专注写业务逻辑然后注意一下方法名就行了,当然也可以用
source.addTransactionalMethod (“*“, txAttr_REQUIRED);匹配所有方法,但是并不是所有的方法都需要事务的。所有推荐匹配方法名的方法
包com.example.demo.aop; 进口org.aspectj.lang.annotation.Aspect; 进口org.springframework.aop.Advisor; 进口org.springframework.aop.aspectj.AspectJExpressionPointcut; 进口org.springframework.aop.support.DefaultPointcutAdvisor; 进口org.springframework.beans.factory.annotation.Autowired; 进口org.springframework.context.annotation.Bean; 进口org.springframework.context.annotation.Configuration; 进口org.springframework.transaction.PlatformTransactionManager; 进口org.springframework.transaction.TransactionDefinition; 进口org.springframework.transaction.interceptor.DefaultTransactionAttribute; 进口org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource; 进口org.springframework.transaction.interceptor.TransactionInterceptor;/* * * @program:德鲁伊 * @description: * @author:陈宇 * @create: 2018-09-20 16:47 * */@Aspect @ configuration 公开课TransactionAdviceConfig { 私有静态最终字符串AOP_POINTCUT_EXPRESSION=爸葱?* com.example.demo.service。* . * (. .)“; @ autowired 私人PlatformTransactionManager transactionManager; @ bean 公共TransactionInterceptor txAdvice () { DefaultTransactionAttribute txAttr_REQUIRED=new DefaultTransactionAttribute (); txAttr_REQUIRED.setPropagationBehavior (TransactionDefinition.PROPAGATION_REQUIRED); DefaultTransactionAttribute txAttr_REQUIRED_READONLY=new DefaultTransactionAttribute (); txAttr_REQUIRED_READONLY.setPropagationBehavior (TransactionDefinition.PROPAGATION_REQUIRED); txAttr_REQUIRED_READONLY.setReadOnly(真正的);=new NameMatchTransactionAttributeSource NameMatchTransactionAttributeSource来源(); source.addTransactionalMethod(“*“保存,txAttr_REQUIRED); source.addTransactionalMethod(“删除*“,txAttr_REQUIRED); source.addTransactionalMethod(“更新*“,txAttr_REQUIRED); source.addTransactionalMethod (“exec *“, txAttr_REQUIRED); source.addTransactionalMethod (“*“, txAttr_REQUIRED); source.addTransactionalMethod (“*“, txAttr_REQUIRED_READONLY); source.addTransactionalMethod(“查询*“,txAttr_REQUIRED_READONLY); source.addTransactionalMethod(“找到*“,txAttr_REQUIRED_READONLY); source.addTransactionalMethod(“*“列表,txAttr_REQUIRED_READONLY); source.addTransactionalMethod(“数*“,txAttr_REQUIRED_READONLY); source.addTransactionalMethod (“*“, txAttr_REQUIRED_READONLY); 返回新TransactionInterceptor (transactionManager来源); } @ bean 公共顾问txAdviceAdvisor () { AspectJExpressionPointcut切入点=new AspectJExpressionPointcut (); pointcut.setExpression (AOP_POINTCUT_EXPRESSION); 返回新DefaultPointcutAdvisor(切入点,txAdvice ()); } }看完这篇关于springboot的aop切面不起作用怎么办的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。
springboot的aop切面不起作用怎么办