使用Spring AOP,我试图在Web应用程序中记录名为corelation的对象,如下所示:-
LoggingCorrelationEnrichingAspect.java:-
@Aspect
@Component
public class LoggingCorrelationEnrichingAspect {
private static final Logger logger = getLogger(LoggingCorrelationEnrichingAspect.class);
@Around("@annotation(Correlated)")
public Object wrapWithCorrelationContext(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
logger.info("Entering "+ proceedingJoinPoint.getSignature().getName() +" with Correlation Id:: "
+ ((Map)proceedingJoinPoint.getArgs()[0]).get(CommerceConnectorConstants.HttpHeaders.CORRELATION_ID).get());
return ((Mono<?>) proceedingJoinPoint.proceed());
}
}
Correlated.java:-
@Inherited
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Correlated {}
在我的主要REST Controller操作中,使用@Correlated
批注,我试图像下面那样记录这种corellation:-
@Correlated
@GetMapping(path = "/products}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Mono<ProductBeanResponse> getProducts(
@RequestHeader(name = Test.HttpHeaders.TENANT_ID, required = true) UUID tId,
@RequestHeader(name = Test.HttpHeaders.CORRELATION_ID, required = true) UUID correlationId
----
---
}
但是,当我使用PostMan工具测试服务并查看应用程序日志时,永远不会记录corelation id:-
logger.info("Entering "+ proceedingJoinPoint.getSignature().getName() +" with Correlation Id:: "
+ ((Map)proceedingJoinPoint.getArgs()[0]).get(CommerceConnectorConstants.HttpHeaders.CORRELATION_ID).get());
请告知这是Spring AOP中的配置问题。
谢谢
答案 0 :(得分:2)
这可以通过以下两种方式之一进行
Correlated
的完全限定名称为@Around("@annotation(com.x.y.z.Correlated)")
更新Aspect方法签名以将Correlated
作为第二个参数
@Around("@annotation(correlated)")
public Object wrapWithCorrelationContext(ProceedingJoinPoint proceedingJoinPoint, Correlated correlated ) throws Throwable {
logger.info("Entering "+ proceedingJoinPoint.getSignature().getName() +" with Correlation Id:: "
+ ((Map)proceedingJoinPoint.getArgs()[0]).get(CommerceConnectorConstants.HttpHeaders.CORRELATION_ID).get());
return ((Mono<?>) proceedingJoinPoint.proceed());
}
在注释中告知是否需要其他内容。
P.S。:另外,正如M. Deinum所指出的,请确保删除对象投射。