SpringBoot的通常配置是直接使用application.properties或者application.yml,由于SpringBoot会读取.perperties和yml文件来覆盖默认配置;html
ResourceProperties
这个class说明了springboot默认读取的propertiesjava
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
WebMvcAutoConfiguration
这个class就是springboot默认的mvc配置类,里面有一个static class实现了WebMvcConfigurer
接口,;具体这个接口有什么用,具体能够看spring官网springMVC配置git
@Configuration @Import(EnableWebMvcConfiguration.class) @EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class }) @Order(0) public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ResourceLoaderAware
能够看到里面的默认viewResolver配置,只要咱们复写了ViewResolver这个bean,就至关于不适用SpringBoot的默认配置;github
@Bean @ConditionalOnMissingBean public InternalResourceViewResolver defaultViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix(this.mvcProperties.getView().getPrefix()); resolver.setSuffix(this.mvcProperties.getView().getSuffix()); return resolver; }
这里里面有一个很常见的mapping,在springboot启动日志中就能够看到。web
@Bean public SimpleUrlHandlerMapping faviconHandlerMapping() { SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping(); mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1); mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler())); return mapping; }
实验证实,经过实现了WebMvcConfigurer
接口的bean具备优先级 (或者继承WebMvcConfigurationSupport
),会覆盖在.properties中的配置。好比ViewResolver
spring
2种方式,1是实现WebMvcConfigurer
接口,2是继承WebMvcConfigurationSupport
;(其实还有第三种,就是继承WebMvcConfigurerAdapter
,可是这种方式在Spring 5中被舍弃)编程
一个整合了mybatis,jsp的SpringBoot编程方式配置在个人GitHub中给出,欢迎你们star;
springboot-config-programmaticallyspringboot