Home > Back-end >  Spring AOP work in one method and doesn't work in another of same class
Spring AOP work in one method and doesn't work in another of same class

Time:10-04

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) {
        //
        //
    }
}

CodePudding user response:

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.

CodePudding user response:

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

  • Related