gpt4 book ai didi

spring-boot - 在 spring boot 2.1.1 自动配置中没有调用 addInterceptors

转载 作者:行者123 更新时间:2023-12-05 00:58:58 25 4
gpt4 key购买 nike

我正在编写一个 spring-boot start,旨在为处理程序自动配置拦截器。主类如下所示:

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(MetricsProperties.class)
public class MetricsConfiguration implements WebMvcConfigurer {
private static Logger logger = LoggerFactory.getLogger(MetricsConfiguration.class);

private final MetricsProperties metricsProperties;

public MetricsConfiguration(MetricsProperties properties) {
this.metricsProperties = properties;
}

@Bean
@Order(0)
public MetricsCenter createMetricsCenter() {
MetricsCenter metricsCenter = MetricsCenter.getInstance();
metricsCenter.init(metricsProperties);
return metricsCenter;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
logger.error("alex add"); // the log does not appear!!
registry.addInterceptor(new MetricsInterceptor());
}
}

根据文件

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration

上面写着:

If you want to keep Spring Boot MVC features and you want to add additional MVC configuration (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, you can declare a WebMvcRegistrationsAdapter instance to provide such components.

我按照文档说的做了,但是没有调用 addInterceptors 并且我的拦截器没有配备spring mvc。

有人可以帮我离开这里吗?

谢谢。

最佳答案

您可能有一个自定义 @Configuration 类(例如,在您的启动器的使用者中)扩展 WebMvcConfigurationSupport 类,在这种情况下,它会完全控制您的 WebMvcConfigurer不会被调用。

关于spring-boot - 在 spring boot 2.1.1 自动配置中没有调用 addInterceptors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55183856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com