0

I am trying to intercept a class in third party jar from my spring boot application.

I am trying to do it as

<context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>

    <bean id="instrumentationLoadTimeWeaver"
          class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>

While my Aspect logger class looks like

@Aspect
public class LogInterceptor
{
    private static final Logger PERF_LOGGER = LoggerFactory.getLogger("PERFLOG");

    private static final Logger LOGGER = LoggerFactory.getLogger(LogInterceptor.class);
    private static final int SYSTEM_ERROR_STATUS_CODE = -1;
    private static final int SYSTEM_SUCCESS_STATUS_CODE = 0;



@Around(
            "execution(* com.alpha.executor.dao..*(..)) || "
                    + "execution(* com.alpha.executor.resource..*(..)) || "
                    + "execution(* com.alpha.executor.client..*(..)) || "
                    + "execution(* com.alpha.executor.service..*(..)) || "
                    + "execution(* com.alpha.executor.proxy.*.*(..)) ")
    public Object logRecordInPerflog(ProceedingJoinPoint joinPoint) throws Throwable
    {

        String activityName = joinPoint.getTarget().getClass().getSimpleName();
        String operationName = activityName + "." + joinPoint.getSignature().getName();
        RequestLog reqLog = new RequestLog(operationName, activityName, joinPoint.getArgs());

        PERF_LOGGER.info(reqLog.getLoggerString());
        try
        {
            Object result = joinPoint.proceed();
            ResponseLog resLog = new ResponseLog(reqLog, result, SYSTEM_SUCCESS_STATUS_CODE);
            PERF_LOGGER.info(resLog.getLoggerString());
            return result;
        }
        catch (Exception ex)
        {
            logException(reqLog, ex);
            throw ex;
        }
    }

    private void logException(RequestLog reqLog, Exception ex)
    {
        ResponseLog resLog;
        if (ex instanceof SvxException)
        {
            SvxException svxException = (SvxException) ex;
            ErrorCode errorCode = svxException.getSvxExceptionDetail().getErrorCode();
            ErrorResponse errorResponse = new ErrorResponse(errorCode,svxException.getSvxExceptionDetail().getErrorDescription());
            resLog = new ResponseLog(reqLog,null,errorCode.getHttpStatus().value(),errorResponse,errorCode.getErrorType().toString());

        }
        else
        {
            LOGGER.error("Error: ", ex);
            resLog = new ResponseLog(reqLog, null, SYSTEM_ERROR_STATUS_CODE, ErrorType.SYSTEM_ERROR.toString(), ex.getMessage());

        }
        PERF_LOGGER.info(resLog.getLoggerString());
    }

}

However, the above code gives an error that No custom load time weaver found. I don't want to use spring-instrumentation.jar in VM args.

Also already tried the class-based method with annotation, however, read somewhere that Spring might be loading that class after some classes are loaded hence that might not work.

EDIT:

Added the logger and advice. Here I have manually created a proxy, see the last line, which isn't a good solution I believe.

codeomnitrix
  • 4,179
  • 19
  • 66
  • 102
  • Just assuming for a moment that what you want is possible (i.e. there is a way of getting LTW working without specifying a Java agent on the JVM command line) and you just have not figured out how to properly configure it, can you confirm that with the weaving agent it does work? Before that is not clear, we cannot proceed any further from here. As you are not showing any aspect code here (only static variables, no pointcuts and advices), nobody can say why it does not work as of now. BTW, I also don't know what you could possibly mean by "class-based method with annotation". – kriegaex May 26 '18 at 08:01
  • @kriegaex added the code above, sorry missed the notification previously. I did get that work with creation of manual proxy, see in the last line in the advice. Please advise if there is a better way to do with load time weaving – codeomnitrix Jun 17 '18 at 14:06
  • Thanks for the update. Unfortunately seeing your aspect and a little config snippet does not help me reproduce a problem consisting of application code, configuration and aspect code. Please add a complete [MCVE](http://stackoverflow.com/help/mcve). You also did not answer my question. Without your manual proxy thing (which you also have not shown in your code) and with regular LTW configured via command line, does your code completely work as intended? – kriegaex Jun 19 '18 at 00:35
  • Sure I will create a sample app and share... – codeomnitrix Jun 19 '18 at 16:11

0 Answers0