SpringBoot基础——SpringBoot拓展SpringMVC

(一)、扩展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");
    }
}

实现原理:

  1. WebMvcAutoConfiguration是SpringMvc的自动配置类
  2. 在做其他自动配置时会导入,@Import(EnableWebMvcConfiguration.class)
  3. 容器中所有的WebMvcConfigurer都会一起起作用

(二)、修改SpringBoot的默认配置

  1. SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean,@Componet)如果有就用用户自己配置的,如果没有,才自动配置;如果有些组件可以有多个(@ViewController),那么将会两者结合起来
  2. 在SpringBoot中会有非常多的xxxWebMvcConfigurer
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值