Pointcut expression 'if()' contains unsupp

2019-08-30 08:49发布

问题:

I am using AspectJ in my Spring boot project for AOP.

I have declared a if() point cut:

public class myPointCuts {
   // a global boolean variable, value can be updated at runtime.
   public static boolean IS_RESULT_FINE;

   @Pointcut("if()")
    public static boolean isResultFine() {
            return IS_RESULT_FINE;
    }
}

At compile time, I get error:

Initialization of bean failed;
nested exception is org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException: Pointcut expression 'if()' contains unsupported pointcut primitive 'if'

My declared dependencies:

implementation 'org.springframework:spring-aop:5.0.1.RELEASE'
implementation 'org.aspectj:aspectjweaver:1.9.4'

What is wrong with my if() point cut expression ?

回答1:

Probably you are trying to use if() in Spring AOP, but as the error message "unsupported pointcut primitive 'if'" implies, if() is not available in Spring AOP, only in AspectJ, which is also explained in the corresponding Spring manual section. You find a positive and negative list there.

If you want to use if() you do need to switch to AspectJ with load-time weaving. But I am not sure it is worth switching the AOP framework for the sake of the if() pointcut which is not much more than syntactic sugar for an if (IS_RESULT_FINE) statement you could also just put right into your advice method.