在springboot中,有很多 xxx Configuration帮助我们扩展配置,只要在源码里看见这种类,就要去知道它扩展了什么功能
eg:自定义视图解析器
package com.hiss.springboot03web.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Locale;
//如果想自定义定制化的功能,只需写这个组件,交给springboot,他就会帮我们自动装配
//扩展 springmvc
@Configuration //让这个类变成配置类
public class MyMvcConfig implements WebMvcConfigurer {
// public interface ViewResolver 实现了视图解析器接口的类,我们就可以把他看作是视图解析器
@Bean
public ViewResolver MyViewResolver(){
return new MyViewResolver();
}
//自定义了一个视图解析器MyViewResolver
public static class MyViewResolver implements ViewResolver {
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
return null;
}
}
}
参考资料:狂神说