SpringBoot默认配置原理:以WebMvcAutoConfiguration为例
默认配置类
@EnableAutoConfiguration会开启SpringBoot的自动配置,并且根据你引入的依赖来生效对应的默认配置。那么,这些默认配置是在哪里定义的呢?为何依赖引入就会触发配置呢?
其实,在我们的项目中已经引入了一个依赖:spring-boot-autoconfigure,其中定义了大量自动配置类:下面截图没截完
我们来看一个我们熟悉的,例如SpringMVC,查看mvc的自动配置类:
WebMvcAutoConfiguration源码
上面的注解中:
@ConditionalOnWebApplication(type = Type.SERVLET)ConditionalOn,就是在某个条件下,此处就是满足项目的类是Type.SERVLET类型,也就是一个普通web工程
接着,我们查看该类中定义了什么:
WebMvcAutoConfiguration的静态内部类WebMvcAutoConfigurationAdapter中
视图解析器:
@Bean @ConditionalOnMissingBean public InternalResourceViewResolver defaultViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix(this.mvcProperties.getView().getPrefix()); resolver.setSuffix(this.mvcProperties.getView().getSuffix()); return resolver; } @Bean @ConditionalOnBean({View.class}) @ConditionalOnMissingBean public BeanNameViewResolver beanNameViewResolver() { BeanNameViewResolver resolver = new BeanNameViewResolver(); resolver.setOrder(2147483637); return resolver; }
上面点击getPrefix,会进入到WebMvcProperties的内部类View
public static class View { private String prefix; private String suffix; public View() { } public String getPrefix() { return this.prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } public String getSuffix() { return this.suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } }
WebMvcAutoConfiguration的静态内部类EnableWebMvcConfiguration中
处理器适配器(HandlerAdapter):
@Bean public RequestMappingHandlerAdapter requestMappingHandlerAdapter(@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager, @Qualifier("mvcConversionService") FormattingConversionService conversionService, @Qualifier("mvcValidator") Validator validator) { RequestMappingHandlerAdapter adapter = super.requestMappingHandlerAdapter(contentNegotiationManager, conversionService, validator); adapter.setIgnoreDefaultModelOnRedirect(this.mvcProperties == null || this.mvcProperties.isIgnoreDefaultModelOnRedirect()); return adapter; } protected RequestMappingHandlerAdapter createRequestMappingHandlerAdapter() { if (this.mvcRegistrations != null) { RequestMappingHandlerAdapter adapter = this.mvcRegistrations.getRequestMappingHandlerAdapter(); if (adapter != null) { return adapter; } } return super.createRequestMappingHandlerAdapter(); }
默认配置属性
这些默认配置的属性来自哪里呢?
内部类WebMvcAutoConfigurationAdapter上的注解
WebMvcProperties的内部类View,找到了内部资源视图解析器的prefix和suffix属性
public static class View { private String prefix; private String suffix; public View() { } public String getPrefix() { return this.prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } public String getSuffix() { return this.suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } }
WebProperties的内部类Resources中,主要定义了静态资源(.js,.html,.css等)的默认查找路径:
public static class Resources { private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"}; private String[] staticLocations; private boolean addMappings; private boolean customized; private final WebProperties.Resources.Chain chain; private final WebProperties.Resources.Cache cache;
如果我们不想配置,只需要引入依赖即可,而依赖版本我们也不用操心,因为只要引入了SpringBoot提供的starter,就会自动管理依赖及版本了。
原文已更新:https://www.cnblogs.com/uncleyong/p/17103602.html
__EOF__
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!