通过返回WebMvcConfigurationSupport 的方式, 默认会覆盖 Spring boot的自动配置, 导致配置失效静态资源无法访问:但是在WebMvcConfigurationadpter(已久过时)这是允许的

@Bean
public WebMvcConfigurationSupport initIndexConfig() { return new WebMvcConfigurationSupport() {
@Override
protected void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
} @Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/resources")
.addResourceLocations("classpath:/static")
.addResourceLocations("classpath:/templates")
.addResourceLocations("classpath:/public");
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
}
};
}

 WebMvcConfigurerAdapter 是一个适配器类,

/** @deprecated */
@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
public WebMvcConfigurerAdapter() {
}

WebMvcConfigurationSupport  提供默认实现 不是一个空方法

public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {

    private static final boolean romePresent;

    private static final boolean jaxb2Present;

    private static final boolean jackson2Present;

    private static final boolean jackson2XmlPresent;

    private static final boolean jackson2SmilePresent;

    private static final boolean jackson2CborPresent;

所以: 采用返回 @Bean的方式, 会覆盖实现的很多默认配置导致不能使用,

正确的使用方法是:

@Configuration
public class AppConfig extends WebMvcConfigurationSupport{ @Override
protected void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
} @Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/resources")
.addResourceLocations("classpath:/static")
.addResourceLocations("classpath:/templates")
.addResourceLocations("classpath:/public");
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
}

你还可以选择 直接实现 

WebMvcConfigurer;

关于支持Webjar Swagger 等Jar包的资源路径
   /**
* 资源路径 映射
*/
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/resources")
.addResourceLocations("classpath:/static")
.addResourceLocations("classpath:/templates")
.addResourceLocations("classpath:/public");
/**
* 支持webjars
*/
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
/**
* 支持swagger
*/
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
super.addResourceHandlers(registry);
}
 

最新文章

  1. JavaScript利用replace更改所有符合条件字符
  2. ASP.NET MVC之下拉框绑定四种方式(十)
  3. jsonp模拟获取百度搜索相关词汇
  4. Java 线程Thread.Sleep详解
  5. IE8支持HTML5的占位符placeholder
  6. Python学习遇到的问题
  7. Android or iOS 运行 meteor App 屏幕一片空白 White screen的解决方法
  8. 将 mp3 等音乐资源以资源形式嵌入 exe 文件中
  9. Python模块 - re
  10. Tomcat中常见线程说明
  11. 通过Excel生成PowerDesigner表结构设计
  12. redis最全配置讲解
  13. PHP获取手机型号
  14. socket - option编程:SO_REUSEADDR
  15. hadoop2.4集群的搭建
  16. 18 subprocess模块(跟操作系统交互)
  17. Fiddler实战深入研究(二)[转载]
  18. PAT乙级01
  19. uva1401 dp+Trie
  20. oracle中 trunc(),round(),ceil(),floor的使用

热门文章

  1. MVVMLight绑定数据
  2. luogu P3152 正整数序列
  3. Xcode9 脚本打包报错
  4. EditText 无法失焦与失焦后键盘不收缩解决方案
  5. 使用dva改造旧项目的数据流方案
  6. Java修炼——文件夹的复制
  7. HDU1847 Good Luck In CET4 Everybody
  8. 洛谷 题解 P1083 【借教室】
  9. 小胖求学系列之-文档生成利器(上)-smart-doc
  10. 一篇文章带你解读Redis分布式锁的发展史和正确实现方式