springboot 自定义扩展springmvc功能

在之前的springmvc中我们可能会需要 视图跳转 拦截器 这些功能
在这里插入图片描述
那在springboot中怎么加入这些功能呢?
在之前的springboot中编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型进行扩展配置SpringMvc中的自定义属性配置。但是当前版本将这个类废弃,根据java8的特性,接口不需要实现类重写接口中的全部方法。所以扩展SpringMvc的属性就可以实现WebMvcConfigurer接口。实现对应的方法就可以了。
如下:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer{
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addViewController("/").setViewName("success");
    }  
}

注意事项 不能继承WebMvcConfigurationSupport
因为springmvc在springboot的主配置中有个条件
在这里插入图片描述
当没有这个类时 才会进行配置 如果我们配置了WebMvcConfigurationSupport 配置就会失效
同时也不能注明@EnableWebMvc
因为这个注解内部也导入了WebMvcConfigurationSupport

@EnableWebMvc将WebMvcConfigurationSupport组件导入进来
导入的WebMvcConfigurationSupport只是SpringMVC基本的功能

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

从上面的图片可以总结出模式:

1)SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如 果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默 认的组合起来;

2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置

3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置

猜你喜欢

转载自blog.csdn.net/wmh1152151276/article/details/88011594