0

I have aspect to do something but I need to be able to disable it i .properties file. I tried @ConditionalOnExpression using solution here...

@Component
@Aspect
@ConditionalOnExpression("${enabled:false}")
public class OtherAspect {

    @Pointcut("execution(* *(..))")
    public void methodExecution() {
    }

    @Before("methodExecution()")
    public void adviceMethod(JoinPoint joinPoint) {
        System.out.println("OtherAspect 1");
        if(true){
            System.out.println("OtherAspect 2");
        }
    }
}

I tried @Value()...

@Component
@Aspect
public class OtherAspect {

    @Value("${performance.log.enabled:true}")
    private boolean enabled;

    @Pointcut("execution(* *(..))")
    public void methodExecution() {
    }

    @Before("methodExecution()")
    public void adviceMethod(JoinPoint joinPoint) {
        System.out.println("OtherAspect 1");
        if(enabled){
            System.out.println("OtherAspect 2");
        }
    }
}

I tried @Profile

@Component
@Aspect
@Profile("enabled")
public class OtherAspect {

    @Pointcut("execution(* *(..))")
    public void methodExecution() {
    }

    @Before("methodExecution()")
    public void adviceMethod(JoinPoint joinPoint) {
        System.out.println("OtherAspect 1");
    }
}

Nothing worked. How can I disable at least execution of adviceMethod by application.properties or any other .properties file flag?

JiKra
  • 1,618
  • 3
  • 29
  • 45
  • Since you use Spring-Boot, the `@ConditionalOnExpression` annotation should work without any additional configuration - just make sure your property file is located in the correct `resources` folder. Also the `@EnableWebMvc` annotation usage changes the behavior of resources loading. – Nikolas Charalambidis Sep 13 '18 at 18:25
  • Does it work if I don't have any property file created or if there is no such attribute in it? It should take the default parameter which is false, shouldn't it? I tried it like you see but my aspect always emerged. – JiKra Sep 14 '18 at 08:45

0 Answers0