gpt4 book ai didi

java - 混淆 WebMvcConfigurationSupport 和 WebMvcConfigurerAdapter

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:06:59 25 4
gpt4 key购买 nike

我想在 Windows 中使用 WebMvcConfigurerAdapter 添加资源处理程序,但在 Linux 中它不起作用,所以我添加了 WebMvcConfigurationSupport

调试和测试后我发现两个bean将在两个操作系统中创建,但是WebMvcConfigurerAdapter的覆盖功能将只在Windows上执行,WebMvcConfigurationSupport的覆盖功能将仅在 Linux 上执行。

我找不到原因。两个配置类如下所示:

@Configuration
public class JxWebAppConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("file:"+System.getProperty("user.dir")+"/src/main/webapp/");
super.addResourceHandlers(registry);
}
}

这是另一个:

@Configuration
public class JxWebConfiguration extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("file:"+System.getProperty("user.dir")+"/src/main/webapp/");
super.addResourceHandlers(registry);
}
}

@EnalbeMvc 已经添加到主类

最佳答案

如前所述in the @EnableWebMvc Documentation :

Adding this annotation to an @Configuration class imports the Spring MVC configuration from WebMvcConfigurationSupport

{..}

To customize the imported configuration, implement the interface WebMvcConfigurer or more likely extend the empty method base class WebMvcConfigurerAdapter and override individual methods

{..}

If WebMvcConfigurer does not expose some advanced setting that needs to be configured, consider removing the @EnableWebMvc annotation and extending directly from WebMvcConfigurationSupport

所以实际上要么:

  1. @EnableWebMvc + extending WebMvcConfigurerAdapter(建议第一个选项)
  2. 直接从 WebMvcConfigurationSupport 扩展(完全控制的回退替代方案)

(两种情况都需要@Configuration)

关于java - 混淆 WebMvcConfigurationSupport 和 WebMvcConfigurerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40886095/

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