Announcement Announcement Module
Collapse
No announcement yet.
Turning off Spring aspect at run-time. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Turning off Spring aspect at run-time.

    I have an aspect that I'd like to toggle on or off at runtime. I tried using this code with the idea that ToggleAspect.toggle = false would turn off the aspect:

    Code:
    public class ToggleAspect {
    	public static boolean toggle = true;
    
    	@Pointcut("if() && execution(* com.x.y.z.*.*(..))")
    	public static boolean someMethodExecution() {
    		return toggle;
    	}
    
            @Around("someMethodExecution()")
    	public Object performSomeMethodExecution(ProceedingJoinPoint pjp) throws Throwable {
                    // Do some aspecty stuff here
                    return pjp.proceed();
             }
    This resulted in an exception:

    org.aspectj.weaver.tools.UnsupportedPointcutPrimit iveException: Pointcut expression 'if() && execution(* com.x.y.z.*.*(..))' contains unsupported pointcut primitive 'if'.

    I guess I could use this code,

    Code:
    public Object performSomeMethodExecution(ProceedingJoinPoint pjp) throws Throwable {
                    if(toggle) {
                    // Do some aspecty stuff here
                    }
                    return pjp.proceed();
    }
    but if anyone has a better suggestion, I would appreciate it. Thanks.

  • #2
    Originally posted by grenny View Post

    Code:
    public class ToggleAspect {
    	public static boolean toggle = true;
    
    	@Pointcut("if() && execution(* com.x.y.z.*.*(..))")
    	public static boolean someMethodExecution() {
    		return toggle;
    	}
    
            @Around("someMethodExecution()")
    	public Object performSomeMethodExecution(ProceedingJoinPoint pjp) throws Throwable {
                    // Do some aspecty stuff here
                    return pjp.proceed();
             }
    I am assuming you are doing if(toggle) and not just if() right? In Spring AOP, if () is not supported pointcut expression. Read section 6.2.3.1. Supported Pointcut Designators of the reference manual.

    Comment


    • #3
      Let me rephrase my question... I am currently using if(toggle) in the body of my Around advice to run the code. I was looking to see if there was another (better?) way to do this, like the if() directive in AspectJ.

      Comment

      Working...
      X