Search code examples
javaspring-bootaopaspectj

Aspectj functions through external library


I have an external library which is not an spring application. It defines few Aspectj functions/PointCuts but it is not getting called when i am using this library in an spring boot application. When i define the pointcuts in spring boot application itself it is working fine. I guess something is wrong with my configuration like @EnableAspectJAutoProxy not working or something.

Code of service-:

@EnableAspectJAutoProxy
@Service
public class LtRequest {
    RestTemplate restTemplate;

    public LtRequest() {
        restTemplate = new RestTemplate();
        restTemplate.setErrorHandler( new LtResponseErrorHandler());
    }

    public Object request(String url, LtRequestType type, Class clz){
        return null;
    }
    public RestResponseDTO getObject(String url,Class clz){
        RestResponseDTO restResponseDTO =restTemplate.getForObject(url,RestResponseDTO.class);
        //LtUtil.parseRequest(restResponseDTO);
        return restResponseDTO;
    }

    private  void postObject(String url,Object obj){
        RestResponseDTO restResponseDTO =restTemplate.postForObject(url,obj,RestResponseDTO.class);

    }




}

Code of aspectj pointcut

@Aspect
@Component
public class ParseRequestAspect {
    static final Logger logger = LogManager.getLogger(ParseRequestAspect.class);
    @Around("execution(* com.limetray.helper.utils.LtRequest.getObject(..))")
    public Object parseRequestAfterReturn(ProceedingJoinPoint joinPoint) {
        logger.info("calling after return aspect function..");
        try{
            RestResponseDTO response = (RestResponseDTO) joinPoint.proceed();
            LtUtil.parseRequest(response);
            return response.getResult();
        }
        catch (Throwable e){
            throw new LtException("Exception occured while parsing request response..");

        }


    }
    @After("execution(* com.limetray.helper.utils.LtRequest.getObject(..))")
    public void parseRequestAfterReturn(JoinPoint joinPoint) {
        logger.info("calling after return aspect function..new");
        try{
            /*RestResponseDTO response = (RestResponseDTO) joinPoint.proceed();
            LtUtil.parseRequest(response);
            return response.getResult();*/
        }
        catch (Throwable e){
            throw new LtException("Exception occured while parsing request response..");

        }


    }
}

Both of these classes are in my external library but its not working as expected. Please help.


Solution

  • If you want to use native AspectJ aspects from within a Spring application and not proxy-based Spring AOP, you need to configure your application to use LTW (load-time weaving) via @EnableLoadTimeWeaving or <context:load-time-weaver/>, respectively. See the Spring manual for more information.