【问题标题】:Spring AOP : How to get Caller method arguments values into Spring Aspect class advice methodSpring AOP:如何将调用方方法参数值获取到 Spring Aspect 类建议方法中
【发布时间】:2017-11-19 02:48:09
【问题描述】:

当我问这个问题时,我没有找到任何满足我要求的解决方案。

要求:

目前我正在使用带有 Rest 和 Spring 集成的 Spring Boot 进行项目。

我的要求是将所有网关服务调用记录到审计表中,以了解两种情况的成功和失败。每个网关都包含必须存储在审计表中的网关特定信息。我正在使用 spring AOP - @After Advice 注释来记录审计信息,但是这个注释方法需要审计值,这些值是网关服务 CALLER 方法的一部分。

例子:

@Autowired
com.study.pattern.sample.app.gateway.EmployeGatewayservice employeGatewayservice;

public EmployeOutput getEmployeInformation(EmployeInput employeInput,AuditInput auditInput)
{
    employeGatewayservice.getEmployeInfomation(employeInput);
}

如何在我的建议中获取 auditInput 对象。

@Pointcut("execution(* com.study.pattern.sample.app.gateway..*(..))")
     protected void gateWayCalls()
     {

     }

      @After("gateWayCalls()")
      public void logAfter(JoinPoint joinPoint) {
         **// Here i need auditInput Object**
     }

我在调用 getEmployeInformation() 上的通知方法时受到限制 因为某些服务方法包含非网关调用逻辑。

就我而言,我必须在网关上使用 Aspect 而不是在服务方法上。

我正在寻找一种解决方案,如何将 Caller 方法参数放入我的 spring aop @After 建议中?

【问题讨论】:

    标签: spring spring-integration aop aspectj spring-aop


    【解决方案1】:

    考虑使用MethodInterceptor:

        public Object invoke(MethodInvocation invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            return invocation.proceed();
        }
    

    【讨论】:

    • 谢谢Bilan,我试过了,但没用,但我发现其他选项用这些值创建了我自己的自定义审计注释,我用审计注释注释了网关方法,这些值我在我的方面检索。
    • 嗨@Maddy,你能详细说明你是怎么做到的吗?
    • 抱歉迟到了会分享我的 git 链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多