springboot自动装配

一、约定大于配置

严格遵守springboot的约定,则不需要进行配置,简化配置

如静态资源路径,springboot默认 依次去classpath:/resources/、classpath:/static/、classpath:/public/下面加载静态资源,你把静态资源放在对应目录即可,不需像springmvc需要配置静态资源路径,如果你不遵守约定,放在别的地方,则访问不到

二、自动装配

1. @SpringBootApplication 注解标注springboot启动类

@SpringBootApplication是一个复合注解,包括三个核心注解:@SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan

自动装配核心通过@EnableAutoConfiguration实现

2. springboot启动时,@EnableAutoConfiguration 通过 @Import(AutoConfigurationImportSelector.class) 注入 AutoConfigurationImportSelector

3. AutoConfigurationImportSelector 调用 getCandidateConfigurations() 方法,通过 classpath:/META-INF/spring.factories 文件,查找所有备选配置,

如 WebMvcAutoConfiguration,命名规则统一,都是xxxAutoConfiguration

4. spring.factories中的备选配置很多,通过@ConditionalOnClass、@ConditionalOnMissingBean等条件是否满足决定是否加载该配置项,比如引入spring-boot-starter-web,则满足WebMvcAutoConfiguration的加载条件

@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
    。。。

    public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
        private static final Log logger = LogFactory.getLog(WebMvcConfigurer.class);
        private final Resources resourceProperties;
        private final WebMvcProperties mvcProperties;
        private final WebProperties webProperties;
        private final ListableBeanFactory beanFactory;
        private final WebMvcRegistrations mvcRegistrations;
        private final WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer;
        private ResourceLoader resourceLoader;

    。。。
}

5. 与application.yml等配置文件关系:spring.factories中的每一个备选配置(xxxAutoConfiguration)都对应一个xxxProperties,xxxProperties定义了配置项属性的前缀和名称,

如WebMvcAutoConfiguration对应一个WebMvcPropertirs,WebMvcPropertirs中定义了属性前缀和属性名称:

@ConfigurationProperties(
    prefix = "spring.mvc"
)
public class WebMvcProperties {
    private org.springframework.validation.DefaultMessageCodesResolver.Format messageCodesResolverFormat;
    。。。

    public WebMvcProperties() {
        this.localeResolver = WebMvcProperties.LocaleResolver.ACCEPT_HEADER;
        this.format = new WebMvcProperties.Format();
        this.dispatchTraceRequest = false;
        this.dispatchOptionsRequest = true;
        this.ignoreDefaultModelOnRedirect = true;
        this.publishRequestHandledEvents = true;
        this.throwExceptionIfNoHandlerFound = false;
        this.logResolvedException = false;
        this.staticPathPattern = "/**";
        this.async = new WebMvcProperties.Async();
        this.servlet = new WebMvcProperties.Servlet();
        this.view = new WebMvcProperties.View();
        this.contentnegotiation = new WebMvcProperties.Contentnegotiation();
        this.pathmatch = new WebMvcProperties.Pathmatch();
    }

    。。。
}

这样,列表中的属性我们可以在application.properties中自定义配置,比如,spring.mvc.static-path-pattern=xxx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值