简体   繁体   English

Spring AOP:具有@Transactional注释方法的方法的注释切入点?

[英]Spring AOP : Annotated pointcuts for a method with @Transactional annotated method?

In my businesslogic methods , I have used annotations as follows for Transaction management. 在我的业务逻辑方法中,我对事务管理使用了以下注释。

@Service
public class SampleBusinessLogicImpl implements SampleBusinessLogic {


    @Override
    @Transactional(rollbackFor=Exception.class)
    public Sample createSample(Sample sample) throws SampleException {
             ...
        }
}

I wanted to execute another aspect for this method , I defined an aspect as follows : 我想为此方法执行另一个方面,我定义了一个方面,如下所示:

@Aspect
public class SampleDynamicValidationAspect {

    private static final Logger logger = LoggerFactory.getLogger(RequestValidationAspect.class); 


    @Before("execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.createSample(com.rakuten.gep.sample.entity.common.Sample,..)) && args(sample,..) throws *Exception")
    public void validate(Sample sample) throws SampleException {
        //Dynamic validation here.
        //If some validation is failed, wrapped the appropiate exception in SampleException
        logger.debug("Involking Dynamic Validator");
        System.out.println("************** Dynaic Validator *************");
    }
}

I can see Transactions works fine looking at the logs , but this newly declared advise doesn't get executed. 我可以看到事务在查看日志时工作正常,但是此新声明的通知未得到执行。

Any things ? 有事吗

尝试将validate(Sample)更改为validate(JointPoint)

You can achieve this in different ways. 您可以通过不同的方式来实现。 For example, you can find different pointcut options that you can test. 例如,您可以找到可以测试的不同切入点选项。 Below you can find different options you can try to make your aspect work. 在下面,您可以找到不同的选项,可以尝试使方面发挥作用。

By executing everything matching @Transactional 通过执行与@Transactional匹配的所有内容

execution(@Transactional * *.*(..))

By executing every public method (you can use this to test if you have your Aspect framework configured well). 通过执行每个公共方法(您可以使用它来测试您的Aspect框架是否配置正确)。

execution(public * *(..))

By adding a pointcut like this and then apply it to your aspect: 通过添加这样的切入点,然后将其应用于您的方面:

@Pointcut("execution(@Transactional* *.*(..))")
public void monitorRequestTargets(){}

@Around("monitorRequestTargets()")
public void validate(Sample sample) throws SampleException {

Let me know if any of then worked ok. 让我知道是否可以正常工作。

您的切入点表达式应为

@Before("execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.createSample(com.rakuten.gep.sample.entity.common.Sample,..) throws *Exception) && args(sample,..)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Spring AOP 从带注释的方法中获取带注释的参数值 - Get annotated param value from annotated method using Spring AOP Spring AOP方面不拦截带注释的方法 - Spring AOP aspect does not intercept annotated method 回滚 @Transactional 注释的方法 - Rollback a @Transactional annotated method 从另一个@Transactional注释方法调用@Transactional注释方法 - Call @Transactional annotated method from another @Transactional annotated method 与方法注解者@Transactional的通信 - Communication to caller of method annotated @Transactional 如何使用Spring AOP记录使用@RequestMapping注释的方法的成本时间? - How to use spring aop to log cost time of method annotated with @RequestMapping? 在实体上没有合并调用的Spring事务注释方法 - spring transactional annotated method without merge call on entities 从Spring Boot测试调用的@Caching方法[注有@Transactional]无法正常工作 - @Caching method called from spring boot test [annotated with @Transactional] not working Spring 没有为 @service 注释的 class 创建 bean,当它的方法之一用 @transactional 注释时 - Spring is not creating the bean for @service annotated class when one of its method is annotated with @transactional 在没有AOP的情况下拦截带注释的方法调用 - Intercepting annotated method calls without AOP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM