简体   繁体   中英

Exception using @After(@annotation) pointcut with AspectJ + SpringBoot

Hello there fellow programmers.

I have a SpringBoot Rest API I'm working on and I'm trying to use an Aspect that performs some actions after the execution of a method with a custom annotation.

So I have this custom annotation:

@Retention(RUNTIME)
@Target(METHOD)
public @interface PublishMQ {
    String destinyName() default "";
    boolean skipNull() default true;
}

And I have this aspect:

@Aspect
@Component
public class PublishMQAspect {

// ...
    @After("@annotation(br.com.powertiss.utils.transaction.PublishMQ)")
    public Object publishChangeToMQ(Object returnValue, PublishMQ publishMQ) throws Throwable {
// ...

I'm trying to use them in a service:

@Service
public class OperatorService {
// ...
    @PublishMQ(destinyName = "queues/Opera")
    public Operator salve(Operadora operator) {
// ...

But I'm getting the given exception on startup:

java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut

The PublishMQ and PublishMQAspect are in a separate jar from OperatorService, but I don't believe this should be an issue.

I tried many things for several hours but couldn't understand why AspectJ is raising this exception. Can you guys help? Thanks.

Before I answer, let me constructively criticise your way of asking the question: The question is unclear. Why do you provide incoherent code snippets instead of full classes or methods? Specifically, you do not show which method parameters you actually want to use in your aspect advice. So now I have to guess (which I don't like because now I have 4x more work than necessary, see below). I kindly suggest you to learn what an MCVE is - a 4k reputation user should know already - and try to compose better questions in the future.


You cannot use advice method parameters which do not occur in your pointcut or result, other than a JoinPoint parameter which is always implicitly available. You should read some AspectJ documentation in order to learn more about pointcut and advice syntax.

You have a choice: Either in your advice method you don't need the return value and the annotation...

@After("@annotation(br.com.powertiss.utils.transaction.PublishMQ)")
public Object publishChangeToMQ(JoinPoint thisJoinPoint) throws Throwable {

... or you need the annotation value only ...

@After("@annotation(publishMQ)")
public Object publishChangeToMQ(JoinPoint thisJoinPoint, PublishMQ publishMQ) throws Throwable {

... or you need the return value only...

@AfterReturning(pointcut = "@annotation(br.com.powertiss.utils.transaction.PublishMQ)", returning = "returnValue")
public Object publishChangeToMQ(JoinPoint thisJoinPoint, Object returnValue) throws Throwable {

... or you need both the return value and the annotation...

@AfterReturning(pointcut = "@annotation(publishMQ)", returning = "returnValue")
public Object publishChangeToMQ(JoinPoint thisJoinPoint, PublishMQ publishMQ, Object returnValue) throws Throwable {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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