目录
1.实现无业务逻辑跳转
2.WebMvcConfigurerAdapter方法过时
3.继承WebMvcConfigurationSupport导致静态资源无法访问
4.WebMvcAutoConfiguration Did not match,webmvcAutoConfiguration配置没有加载
5.ThymeleafAutoConfiguration加载条件
1.实现无业务逻辑跳转有的时候时候我们只需要一个业务逻辑的跳转,这时候我们会在Controller中写一个跳转的方法,如下图所示。但是每次需要跳转都需要一个方法太麻烦也不便于管理,于是就有了WebMvcConfigurerAdapter类。
@RequestMapping("/")
public String index(){
return "login";
}
2.WebMvcConfigurerAdapter方法过时
但是自从Spring Boot2.0的版本之后这个方法就过时了,由以下两种方法来实现。
①implements WebMvcConfigurer(官方推荐)
②extends WebMvcConfigurationSupport
/
* @deprecated as of 5.0 {@link WebMvcConfigurer} has default methods (made
* possible by a Java 8 baseline) and can be implemented directly without the
* need for this adapter
*/
@Configuration
public class MyMvcConfigNew extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
}
}
3.继承WebMvcConfigurationSupport导致静态资源无法访问
不知为啥就选择了继承WebMvcConfigurationSupport类,浏览器输入localhost:8080,enter跳转,页面成功访问,但是样式很奇怪,发现静态资源没有加载。
@Configuration
public class MyMvcConfigNew extends WebMvcConfigurationSupport {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
}
}
2020-04-11 10:37:18.514 WARN 21820 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound : No mapping for GET /asserts/css/bootstrap.min.css
2020-04-11 10:37:18.514 WARN 21820 --- [nio-8080-exec-3] o.s.web.servlet.PageNotFound : No mapping for GET /asserts/css/signin.css
2020-04-11 10:37:18.515 WARN 21820 --- [nio-8080-exec-4] o.s.web.servlet.PageNotFound : No mapping for GET /asserts/img/bootstrap-solid.svg
4.WebMvcAutoConfiguration Did not match,webmvcAutoConfiguration配置没有加载
WebMvcAutoConfiguration Did not match,为啥呢,下面提示@ConditionalOnMissingBean,这个提示是容器中不存在指定Bean; 就是说WebMvcAutoConfiguration 中不能有@ConditionalOnMissingBean(xxBean)指定的bean。WebMvcAutoConfiguration 不能有WebMvcConfigurationSupport,然后在我们的myMvcConfigNew继承了WebMvcConfigurationSupport,所以WebMvcAutoConfiguration 没有被加载。
found beans of type 'org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport' myMvcConfigNew
WebMvcAutoConfiguration:
Did not match:
- @ConditionalOnMissingBean (types: org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; SearchStrategy: all) found beans of type 'org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport' myMvcConfigNew (OnBeanCondition)
Matched:
- @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet', 'org.springframework.web.servlet.config.annotation.WebMvcConfigurer' (OnClassCondition)
- found 'session' scope (OnWebApplicationCondition)
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {...}
5.ThymeleafAutoConfiguration加载条件
注意AutoConfigureAfter注解,它表明在加载配置的类之后再加载当前类,之前WebMvcAutoConfiguration 没有加载,导致ThymeleafAutoConfiguration没有加载,这是导致我们静态资源无法加载的元凶。ThymeleafAutoConfiguration标明了@EnableConfigurationProperties(ThymeleafProperties.class),会去ThymeleafProperties加载配置文件,在ThymeleafProperties指定了静态资源会从classpath:/templates/这个路径下加载。ThymeleafAutoConfiguration没有加载,导致我们无法从classpath:/templates/下获取静态文件。
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnClass({ TemplateMode.class, SpringTemplateEngine.class })
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class })
public class ThymeleafAutoConfiguration {...}
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
...}
作者:benbenniaono1