简体   繁体   English

现有注释的 AspectJ 表达式,注释之后但方法执行之前的切入点

[英]AspectJ Expression for an existing annotation, pointcut after annotation but before method execution

Here's the existing code:这是现有的代码:

@Transactional
//  <-- Want to Pointcut to here, after Transactional is done, before method execution
public String getUsername(int userId) {
     ...
}

Trying to pointcut to the commented line, here's what I tried so far:试图切入注释行,这是我到目前为止所尝试的:

@Around("@annotation(org.springframework.transaction.annotation.Transactional) " +
        " && !target(org.springframework.transaction.interceptor.TransactionAspectSupport)")
public Object runQueryWithinTransaction(ProceedingJoinPointjp) throws Throwable {
     ...
}

Also tried setting the order of my aspect to LowestPrecedence.还尝试将我的方面的顺序设置为 LowestPrecedence。 Despite all of this, I still see my Aspect being called before Transactional's "invokeWithinTransaction" method.尽管如此,我仍然看到我的 Aspect 在 Transactional 的“invokeWithinTransaction”方法之前被调用。 Tried "@After" but that was executed after my "getUsername" method.尝试了“@After”,但这是在我的“getUsername”方法之后执行的。 Tried "@Before", for no reason, but that obviously didn't help.无缘无故地尝试了“@Before”,但这显然没有帮助。

What should I be doing instead?我应该怎么做?

Solved this by setting precedence in my aspect like so:通过在我的方面设置优先级来解决这个问题,如下所示:

@Aspect
@DeclarePrecedence("org.springframework.transaction.aspectj.AnnotationTransactionAspect, *")
public class MyAspect {
    ....
}

Thanks to @kriegaex, in case you aren't using AspectJ and regular Spring AOP, you should be trying to use @Ordered and @Order annotations.感谢@kriegaex,如果您不使用AspectJ 和常规Spring AOP,您应该尝试使用@Ordered@Order注释。 @Order on Spring Transactional annotation can be set in @EnableTransactionManagement annotation, and set a lower precedence (or none at all, since lowest precedence is default) for your own Aspect. @Order on Spring Transactional annotation 可以在@EnableTransactionManagement annotation 中设置,并为您自己的 Aspect 设置较低的优先级(或根本没有,因为最低优先级是默认值)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM