Can we enable or disable Aspect based on value of any flag or through configuration file?

后端 未结 3 1616
梦如初夏
梦如初夏 2020-12-18 01:22

I have added following dependency in pom.xml


            org.springframework
                     


        
3条回答
  •  生来不讨喜
    2020-12-18 01:54

    There is always the if() pointcut expression that allows you to define a condition to be checked by the advice: https://www.eclipse.org/aspectj/doc/next/adk15notebook/ataspectj-pcadvice.html#d0e3697

    And... your aspect already is a Spring @Component. Why not just inject properties via @Value and decide on them whether your advice should execute? You can do that via the if() pointcut described above or by just checking the value in the advice and either executing or skipping because of it.

    @Component
    @Aspect
    public class AuthenticationServiceAspect {
    
        @Value("${aspect.active}")
        private boolean active = true;
    
        @Pointcut("execution(* com.service.impl.AuthenticationServiceImpl.*(..)) && if()")
        public static boolean conditionalPointcut() {
            return active;
        }
    
        @Before("conditionalPointcut()")
        public void adviceMethod(JoinPoint joinPoint) {
            if(true){
                throw new Exception();
            }
        }
    }
    

提交回复
热议问题