SpringBoot基础——SpringBoot拓展Spring

2019-08-06  本文已影响0人  XHHP

(一)、扩展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
上一篇下一篇

猜你喜欢

热点阅读