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"); } }