【问题标题】:Whether it is possible to write AOP for Spring RestTemplate class and any external jar classes using spring AOP or Aspectj是否可以为 Spring RestTemplate 类和任何使用 spring AOP 或 Aspectj 的外部 jar 类编写 AOP
【发布时间】:2016-11-04 17:03:52
【问题描述】:

是否可以使用 spring AOP 或 Aspectj 为 Spring RestTemplate 类编写 AOP。例如:

@Around("execution(* org.springframework.web.client.RestTemplate.getFor*(..))")  

谢谢

【问题讨论】:

标签: spring aspectj spring-aop aspectj-maven-plugin


【解决方案1】:

我遇到了同样的问题,无法使用 AOP。

但是,在这种情况下,有一种解决方法。由于RestTemplate 扩展了InterceptingHttpAccessor,因此您可以拦截所有来自RestTemplate 对象的请求。

记录所有 HTTP 请求的示例配置:

@Bean
public RestTemplate restTemplate() {

    RestTemplate restTemplate = new RestTemplate();

    // (...)
    // setup code for the RestTemplate object

    restTemplate.getInterceptors().add((request, body, execution) -> {
        logger.info("HTTP {} request to {}", request.getMethod(), request.getURI());
        return execution.execute(request, body);
    });

    return restTemplate;
}

虽然这不等同于使用方面,但您可以通过拦截器和非常少的配置获得类似的功能。

【讨论】:

    猜你喜欢
    • 2015-12-17
    • 2012-03-28
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多