0

I am using Spring AOP for custom annotation, where it trigger fine in other method but don't work in below mentioned method. I am new to AOP concept so please help. I have tried to use same AOP on other method which is working perfectly fine. I don’t know this is happening. Early I came to know for jdk proxy, calling method must be public so I changed that even after doing that is didn’t resolve my problem.

Custom annotation:

package com.abc.xyz.common.logger;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UPIPGLog {
    String type() default "OTHER";

    boolean logParams() default true;

    String[] skipRegexInResponseLog() default {};

    String[] maskInResponseLog() default {};
}

Aspect:

package com.abc.xyz.common.logger;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.EnumUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Aspect
@Order(3)
public class UPIPGAutologger{

    @Around("execution(@com.abc.xyz.common.logger.UPIPGLog * *(..)) && @annotation(upipgLog)")
    public Object performanceLog(ProceedingJoinPoint joinPoint, UPIPGLog upipgLog) {
        Object response = null;
        -
        -
        -
    }

}

Calling method:

package com.abc.xyz.handler.gateway.impl.opt.idk;

@Component
public class ABC implements XYZ {

    @UPIPGLog
    @Override
    public  <RESPONSE> RESPONSE getAPIResponse(LogType logType, Integer timeoutInSeconds, String url,
            String payload, Class<RESPONSE> responseClass, HTTPRequestType requestType, String contentType,
            Map<String, Object> headers, Map<PGConfigParameters, Object> config) {
        //
        //
    }
}
Matt Ke
  • 3,599
  • 12
  • 30
  • 49
  • Information while research I came to know - for jdk proxy resolution - method for AOP must be public and for cglib proxy - method can either be protected or public . – Shubham Gangwar Oct 03 '21 at 19:49
  • 1
    And `getAPIResponse` must be called by another component, if you just call it internally `public void someMethod(){ this.getAPIResponse(...) }` the simple proxy based mechanism won't work. Local or internal method calls within an advised class doesn’t get intercepted by the proxy, so advise method of the aspect does not get fired/invoked – Andreas Oct 03 '21 at 20:48
  • Thanks @Andreas. It resolved my issue – Shubham Gangwar Oct 03 '21 at 22:38

2 Answers2

1

getAPIResponse must be called by another component, if you just call it internally the simple proxy based mechanism won't work.

Example:

public void someMethod() {
    this.getAPIResponse(...)
}

Local or internal method calls within an advised class doesn’t get intercepted by the proxy, so advise method of the aspect does not get fired/invoked.

I was calling method from the internal class method only. Thanks @Andreas for mentioning in comments.

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
0

try using fully qualified path inside of @annotation, @annotation(com.abc.xyz.common.logger.UPIPGLog)

J Asgarov
  • 2,526
  • 1
  • 8
  • 18