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

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

I have added following dependency in pom.xml


            org.springframework
                     


        
3条回答
  •  时光取名叫无心
    2020-12-18 01:58

    You may use enabling/disabling component with properties with ConditionalOnExpression annotation. When component is disabled the aspect is too.

    @Component
    @Aspect
    @ConditionalOnExpression("${authentication.service.enabled:true}")// enabled by default
    public class AuthenticationServiceAspect {
        @Before("execution(* com.service.impl.AuthenticationServiceImpl.*(..))")
        public void adviceMethod(JoinPoint joinPoint) {
            if(true){
                throw new Exception();
            }
        }
    }
    

    To disabling the aspect, just add authentication.service.enabled=false to your application.properties.

提交回复
热议问题