SpringMVC自动配置【有些未完成的原理待研究】

SpringBoot通过WebMvcAutoConfiguration自动配置好了SpringMVC

SpringMVC自动配置原理

待研究

扩展SpringMVC

SpringBoot为SpringMVC做了很多自动配置,比如ContentNegotiatingViewResolver `等,但是还有一些还是需要我们去自己实现的,比如下面:
在这里插入图片描述
如果我们想要实现这样的扩展,我们可以编写一个配置类(@Configuration),是WebMvcConfigurer类型;不能标注@EnableWebMvc

实验

1、IDEA快速创建一个SpringBoot
2、引入模板引擎Thymeleaf

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

备注:如果需要查看html文件,必须先引入模板引擎

3、扩展实例

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("helloSpringMVC").setViewName("success");
    }

}

4、src\main\resources\templates\success.html的内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Tiaaaaaaaaaaaaaaaatle</title>
</head>
<body>
aaaaaaaav dfdeswqazxsdcfvaaaaaaaa
</body>
</html>

5、浏览器访问:http://localhost:8080/helloSpringMVC

原理【待研究】

发布了254 篇原创文章 · 获赞 70 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/zhizhengguan/article/details/103990577