(一)、扩展SpringMVC
<mvc:view-controller path="/hello" view-name="success"></mvc:view-controller>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean></bean>
</mvc:interceptor>
</mvc:interceptors>
在springboot中进行扩展:编写一个配置类(@Configuration),继承修改WebMvcConfigurerAdapter类,同时不能标注@EnableWebMvc即可
package com.crud.springboot.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
1. MyMvcConfig class
2. 3. @author Flc
3. @date 2019/6/17
*/
//使用WebMvcConfigurerAdapter可以扩展SpringMvc的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送springboot请求,来到success页面
registry.addViewController("/springboot").setViewName("/hello");
}
}
实现原理:
- WebMvcAutoConfiguration是SpringMvc的自动配置类
- 在做其他自动配置时会导入,@Import(EnableWebMvcConfiguration.class)
- 容器中所有的WebMvcConfigurer都会一起起作用
(二)、修改SpringBoot的默认配置
- SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean,@Componet)如果有就用用户自己配置的,如果没有,才自动配置;如果有些组件可以有多个(@ViewController),那么将会两者结合起来
- 在SpringBoot中会有非常多的xxxWebMvcConfigurer
网友评论