前些天棧長在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..}@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 教程,都是實戰幹貨,以下僅為部分預覽。
最近幹貨分享
面試問我 Java 逃逸分析,瞬間被秒殺了。。
到底什麼是重入鎖,拜托,一次搞清楚!
圖解 Java 垃圾回收機制,寫得非常好!
如何寫出讓同僚無法維護的代碼?
分享一份Java架構師學習資料