我试图从我的春季启动应用程序中拦截第三方jar中的一个类。
我正在尝试将其作为
<context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
<bean id="instrumentationLoadTimeWeaver"
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
虽然我的Aspect记录器类看起来像
@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());
}
}
但是,上面的代码给出了一个错误,即找不到自定义加载时间编织器。我不想在VM args中使用spring-instrumentation.jar。
也已经尝试过带有注释的基于类的方法,但是,在某些类加载后可能会加载该类,因此可能无效。
编辑:
添加了记录器和建议。在这里,我手动创建了一个代理,请参阅最后一行,我认为这不是一个好的解决方案。