10

I have added following dependency in pom.xml

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.5</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.5</version>
        </dependency>

And enable AspectJ in appContext.xml as follows:

And define aspect as follows:

@Component
@Aspect
public class AuthenticationServiceAspect {


@Before("execution(* com.service.impl.AuthenticationServiceImpl.*(..))")
    public void adviceMethod(JoinPoint joinPoint) {

        if(true){
            throw new Exception();
        }


}

Now I want to disable this AOP so that above code won't get execute? How can I do this?

user2758176
  • 193
  • 1
  • 7
  • 20

3 Answers3

28

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.

Eric Georges
  • 281
  • 3
  • 3
1

It appears Spring does not support AspectJ's if() pointcut primitive.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'foo': Initialization of bean failed; nested exception is org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException: Pointcut expression 'execution(* doSomething(..)) && if()' contains unsupported pointcut primitive 'if'

chris85
  • 23,846
  • 7
  • 34
  • 51
datree
  • 483
  • 3
  • 7
  • 14
-1

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();
        }
    }
}
kriegaex
  • 63,017
  • 15
  • 111
  • 202
sheltem
  • 3,754
  • 1
  • 34
  • 39
  • This doesn't work, because `active` is not static and hence cannot be returned from the static method `conditionalPointcut`. – Mack Apr 06 '17 at 20:46