第四十一章 SpringBoot SpringMVC配置

来源:互联网 发布:mac书名号怎么打出来 编辑:程序博客网 时间:2024/04/30 12:35

Spring Boot为Spring MVC提供的auto-configuration适用于大多数应用,并在Spring默认功能上添加了以下特性:

  1. 引入 ContentNegotiatingViewResolver 和 BeanNameViewResolver
    beans。
  2. 对静态资源的支持,包括对WebJars的支持。
  3. 自动注册 Converter , GenericConverter , Formatter beans。
  4. 对 HttpMessageConverters 的支持。
  5. 自动注册 MessageCodeResolver 。
  6. 对静态 index.html 的支持。
  7. 对自定义 Favicon 的支持。
  8. 自动使用 ConfigurableWebBindingInitializer bean。

静态资源

1>类路径文件
Spring Boot从classpath下的 /static ( /public , /resources 或 /META-INF/resources )文件夹,或从 ServletContext 根目录提供静态内容。可以通过http://localhost:8080/**来访问。
通过自定义 WebMvcConfigurerAdapter 并覆写 addResourceHandlers 方法来改变该行为(加载静态文件)。
设置 spring.resources.staticLocations 属性自定义静态资源的位置

2>webjar
任何在 /webjars/** 路径下的资源都将从jar文件中提供,只要它们以Webjars的格式打包。

静态index.html的支持

把静态index.html放在如下目录(SpringBoot自动加载的静态资源目录下)
>
classpath:/META-INF/resources/index.html
classpath:/resources/index.html
classpath:/static/index.html
classpath:/public/index.html
>

如果保留Spring Boot MVC特性,你只需添加其他的MVC配置(拦截器,格式化处理器,视图控制器等)。你可以添加自己的 WebMvcConfigurerAdapter 类型的 @Configuration 类,而不需要注解 @EnableWebMvc 。如果希望使用自定义的 RequestMappingHandlerMapping , RequestMappingHandlerAdapter ,或 ExceptionHandlerExceptionResolver ,你可以声明一个 WebMvcRegistrationsAdapter 实例提供这些组件。

如果想全面控制Spring MVC,你可以添加自己的 @Configuration ,并使用 @EnableWebMvc 注解。

0 0
原创粉丝点击