在spring boot 2.0以后 WebMvcConfigurerAdapter 这个方法已经过时,通过百度网上的资料之后发现很多人说是改成继承WebMvcConfigurationSupport这个类,这种方式是有问题的,会导致自动配置失效,应该是实现WebMvcConfigurer这个接口
@Configuration
public class MyWebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/orange").setViewName("success");
}
}
为什么是实现WebMvcConfigurer接口而不是实现这个类WebMvcConfigurerAdapter ,通过查看WebMvcAutoConfiguration spring boot的 spring mvc的自动配置类发现 如果实现这个类会导致自动配置失效
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)