当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot 面试,一个问题就干趴下了!(下)

Spring Boot 面试,一个问题就干趴下了!(下)

2019年07月04日  | 移动技术网IT编程  | 我要评论

前些天栈长在java技术栈微信公众号分享一篇文章:spring boot 面试,一个问题就干趴下了!,看到大家的留言很精彩,特别是说"约定大于配置"的这两个玩家。

哈哈,上墙的朋友开不开森?

不错,约定优(大)于配置确实是 spring boot 整个框架的核心思想。

那么怎么理解约定优于配置呢?

百度百科定义:

约定优于配置(convention over configuration),也称作按约定编程,是一种软件设计范式,旨在减少软件开发人员需做决定的数量,获得简单的好处,而又不失灵活性。

总结就是两点:

1、约定一些推荐的默认配置;

2、开发人员只需要规定不符约定的部分;

这样做的好处就是,如果约定的默认配置符合我们的要求,省略即可,反之,再进行额外配置。

从 spring boot 中提供的默认的配置文件(application.properties/yml),再到默认值自动配置,都可以看出约定带来的便利,以及节省大量的配置。

来看下 spring boot 中一个自动配置的源码实例吧:

@configuration
@conditionalonclass({ servlet.class, standardservletmultipartresolver.class,
        multipartconfigelement.class })
@conditionalonproperty(prefix = "spring.servlet.multipart", name = "enabled", matchifmissing = true)
@conditionalonwebapplication(type = type.servlet)
@enableconfigurationproperties(multipartproperties.class)
public class multipartautoconfiguration {

    private final multipartproperties multipartproperties;

    public multipartautoconfiguration(multipartproperties multipartproperties) {
        this.multipartproperties = multipartproperties;
    }

    @bean
    @conditionalonmissingbean
    public multipartconfigelement multipartconfigelement() {
        return this.multipartproperties.createmultipartconfig();
    }

    @bean(name = dispatcherservlet.multipart_resolver_bean_name)
    @conditionalonmissingbean(multipartresolver.class)
    public standardservletmultipartresolver multipartresolver() {
        standardservletmultipartresolver multipartresolver = new standardservletmultipartresolver();
        multipartresolver.setresolvelazily(this.multipartproperties.isresolvelazily());
        return multipartresolver;
    }

}

@configurationproperties(prefix = "spring.servlet.multipart", ignoreunknownfields = false)
public class multipartproperties {

    /**
     * whether to enable support of multipart uploads.
     */
    private boolean enabled = true;

    /**
     * intermediate location of uploaded files.
     */
    private string location;

    /**
     * max file size. values can use the suffixes "mb" or "kb" to indicate megabytes or
     * kilobytes, respectively.
     */
    private string maxfilesize = "1mb";

    /**
     * max request size. values can use the suffixes "mb" or "kb" to indicate megabytes or
     * kilobytes, respectively.
     */
    private string maxrequestsize = "10mb";

    /**
     * threshold after which files are written to disk. values can use the suffixes "mb"
     * or "kb" to indicate megabytes or kilobytes, respectively.
     */
    private string filesizethreshold = "0";

    /**
     * whether to resolve the multipart request lazily at the time of file or parameter
     * access.
     */
    private boolean resolvelazily = false;

    // get/set/etc..

}

这是一个文件上传的自动配置类,约定了:

1、约定了配置参数以 spring.servlet.multipart 前缀开始;

2、约定了很多默认配置,如:默认上传文件大小为 1m;

3、约定了所有的参数配置类名都是 *properties;

4、约定了所有的自动配置类名都是 *autoconfiguration;

5、约定了所有自动配置类配置在:/meta-inf/spring.factories;

等等……

这样我们做一个文件上传操作几乎不用写任何配置了,除非满足不了需求,如:现在文件上传 1m 太小了,再加一行自定义配置即可,我们也可以按约定编写其他自动配置。

如果还不能理解,再来看 maven 怎么做的,maven 简直把约定大于配置的思想体现淋漓尽致。

maven规定了哪个目录放什么文件,哪个文件做什么用,maven会自动去处理,不需要我们再额外配置,其实我们也没有额外配置的需要,至少栈长我现在还没有遇到过。如果这些目录都让你来通过配置文件来配置,而每个项目配置的又不一样,你会不会想要崩溃?

其实这也不是新技术,只是一种设计思想,早在 jdk 1.5 中添加的《java注解》就是很好的体现。

关于 “约定优于配置” 的思想,你还有什么好的想法,欢迎留言分享~

好了,今天的分享就到这里,关注java技术栈微信公众号,在后台回复:boot,获取栈长整理的更多的 spring boot 教程,都是实战干货,以下仅为部分预览。

  • spring boot 读取配置的几种方式
  • spring boot 如何做参数校验?
  • spring boot 最核心的 25 个注解!
  • spring boot 2.x 启动全过程源码分析
  • spring boot 2.x 新特性总结及迁移指南
  • ……

本文原创首发于微信公众号:java技术栈(id:javastack),转载请原样保留本信息。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网