本篇内容介绍了“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 简直把约定大于配置的思想体现淋漓尽致。
“Spring Boot中如何理解约定优于配置问题”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/javaroad/blog/3069345