4、springmvc扩展

//如果,想diy一些定制化的功能,只要写这个组件,然后将他交给spring boot,spring boot就会帮我们自动配置
    //扩展springmvc     dispatchservlet
public class MyMvcConfig implements WebMvcConfigurer {
    @Bean//ViewResolver  实现了视图解析器接口的类,我们就可以把它看作视图解析器
    public ViewResolver MyViewRsolver(){
        return new MyViewRsolver();
    }
    //自定义了一个自己的视图解析器MyViewRsolver
    public static class MyViewRsolver implements ViewResolver{
        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null;
        }
    }
}
//如果我们要扩展springmvc ,官方建议我们这样做
//@EnableWebMvc,这玩意就是导入了一个类DelegatingWebMvcConfiguration从容器中获取所有的webmvcconfig
// 所有需要接管mvc的类都不能加@EnableWebMvc因为@EnableWebMvc下导入了@Import(DelegatingWebMvcConfiguration.class)
//ngWebMvcConfiguration继承于WebMvcConfigurationSupport
//而在Web WebMvcAutoConfiguration中有条件@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
//@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)这个类不存在时生效
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override//视图跳转
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("dudu").setViewName("test");
    }
}

在spring boot中,有非常多的xxx Configuration帮助我们进行扩展配置,只要看见这个东西,我们就要注意了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值