Spring Boot provides auto-configuration for Spring MVC that works well with most applications.html
The auto-configuration adds the following features on top of Spring’s defaults:java
ContentNegotiatingViewResolver
and BeanNameViewResolver
beans.
Converter
, GenericConverter
, and Formatter
beans.
Formatter
:格式化,2017/0/101--->DateHttpMessageConverters
(covered later in this document).
MessageCodesResolver
(covered later in this document).index.html
support.Favicon
support (covered later in this document).ConfigurableWebBindingInitializer
bean (covered later in this document).
在spring MVC中,咱们能够编写xml来配置咱们须要的一些功能,好比拦截器等等。web
you can add your own @Configuration
class of type WebMvcConfigurer
but without @EnableWebMvc
spring
咱们能够在类型是WebMvcConfigurer的类上使用@Configuration注解,而且实现咱们须要的方法;json
class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("test"); return false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } } @Configuration public class MyMvcConfig implements WebMvcConfigurer{ //不须要实现WebMvcConfigurer了 @Bean public WebMvcConfigurer webMvcConfigurer(){ WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer(){ @Override public void addInterceptors(InterceptorRegistry registry) { //拦截全部的请求,springboot帮咱们已经作好了静态资源映射,因此咱们能够不加excludePathPatterns("/static/**"),可是若是出现被拦截了,那就手动放行 registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/login").excludePathPatterns("/static/**"); } }; return webMvcConfigurer; } }
@Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()).addPathPatterns("/xx"); } }
@EnableWebMvc @Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { } }
为何加上@EnableWebMvc注解后spring mvc就是失效了呢?spring-mvc
一、导入DelegatingWebMvcConfigurationspringboot
@Import(DelegatingWebMvcConfiguration.class) public @interface EnableWebMvc { }
二、DelegatingWebMvcConfiguration 继承了 WebMvcConfigurationSupport mvc
@Configuration public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
三、查看 WebMvcAutoConfiguration 源码app
@Configuration @ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class }) //条件判断,组件中有没有WebMvcConfigurationSupport,没有才自动配置 @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10) @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class }) public class WebMvcAutoConfiguration {
不须要通过controlleride
@Configuration public class MvcConfig implements WebMvcConfigurer { public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/home").setViewName("home"); registry.addViewController("/").setViewName("home"); registry.addViewController("/hello").setViewName("hello"); registry.addViewController("/login").setViewName("login"); } }