Spring boot的Spring MVC扩展功能

Spring中
配置interceptors

<mvc:view-controller path="/hello" view-name="success"/>
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/hello"/>
        <bean></bean>
    </mvc:interceptor>
</mvc:interceptors>
<mvc:default-servlet-handler/>

Spring boot实现方式

参考官网
https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications
这里写图片描述
可以扩展Spring MVC功能
编写一个配置类@Configuration

是WebMvcConfigurerAdapter类型,不能标注@EnableWebMvc
既保留了所有的自动配置,也能用自定义扩展的配置

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
//@EnableWebMvc   不要接管SpringMVC
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //浏览器发送 /atguigu 请求来到 success
        registry.addViewController("/atguigu").setViewName("success");
    }
}

实现原理

WebMvcAutoConfiguration
SpringMVC的自动配置类

搜索,WebMvcAutoConfigurationAdapter
这里写图片描述
EnableWebMvcConfiguration
在做其他自动配置时会导入

@Import(EnableWebMvcConfiguration.class)
这里写图片描述
DelegatingWebMvcConfiguration
搜索,setConfigurers

从容器中获取,所有的WebMvcConfigurer
这里写图片描述
addViewControllers
从容器中获取,所有的WebMvcConfigurer

把它们的addViewControllers方法,都调用一遍
这里写图片描述
addViewControllers
将所有的WebMvcConfigurer相关配置,都一起来调用

就都会一起,来起作用
这里写图片描述
容器中
所有的WebMvcConfigurer都会一起起作用
自定义的配置类,也会被调用

自动配置类,其作用的时候,会扫描容器中
所有的配置类,自定义的配置类,也会扫描到
SpringMVC的自动配置,和自定义的扩展配置都会起作用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值