0

I am trying to write an Aspect, trying to implement Pointcut's if() condition but receiving ArrayIndexOutOfBoundsException. here is the code snippet.

@Pointcut("call(* com.aop.Service.activate(..)) && args(iActivate,..) && if()")
    public static boolean saveActivate(Activate iActivate) {
        return true; //if false @before she not be invoked
    };

@Before("saveActivate(iActivate)")
    public void saveActivateBefore(JoinPoint ijoinPoint, ActivateInstallmentRequest iActivateInstallmentRequest) {
        System.out.println("Log from @before");    
}

This code is giving me below exception:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectMapperConfigurer' defined in class path resource [springfox/documentation/spring/web/SpringfoxWebMvcConfiguration.class]: Initialization of bean failed; nested exception is java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1

Can Someone help me what I'm missing here? PS: I have also referred AspectJ.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Raghu
  • 33
  • 6
  • Before we proceed, I want to double-check: Are you 100% sure you use AspectJ and not just Spring AOP? Did you explicitly configure Spring to use load-time weaving? Another question is if your target class name really is `com.aop.service` with a lower-case "s". Usually Java class names start with a capital letter. – kriegaex Oct 23 '20 at 01:27
  • @kriegaex - I'm using Spring AOP in my project. Based on yu comment, I assume conditional if for a Pointcut in only for AspectJ but not Spring AOP? "com.aop.service" is an arbitrary name I've given in the code, no need to concern about that! – Raghu Oct 23 '20 at 02:51

1 Answers1

1

After having verified that you use Spring AOP, the answer is indeed that both if() and call() are not supported, see also the Spring manual section about pointcut designators.

If you want to use those designators, you need to activate AspectJ via LTW (load-time weaving).

A workaround for Spring AOP would be to simply use a regular if at the beginning of your advice method and, if there are no specific reasons for using call(), use execution() instead, which is what most people want anyway. I am not going to elaborate about the difference between call and execution joinpoints here, but you can e.g. consult my answer there for more information.


Update: There are other things which look a little bit strange in your code snippet, but what I explained is the biggest issue for now.

kriegaex
  • 63,017
  • 15
  • 111
  • 202