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