@EnableAutoConfiguration会开启SpringBoot的自动配置,并且根据引入的依赖来生效对应的默认配置, 那么问题来了:
- 这些默认配置是怎么配置的, 在哪里配置的?
- 为何依赖引入就会触发配置呢?
- 这些默认配置的属性来自哪里呢?
WebMvcAutoConfiguration类中获取配置类中的前缀后缀,来获取属性
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public InternalResourceViewResolver defaultViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix(this.mvcProperties.getView().getPrefix());
resolver.setSuffix(this.mvcProperties.getView().getSuffix());
return resolver;
}
}