[英]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.