135-1821-9792

SpringBoot中如何理解约定优于配置问题

本篇内容介绍了“Spring Boot中如何理解约定优于配置问题”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

十余年的广东网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整广东建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“广东网站设计”,“广东网站推广”以来,每个客户项目都认真落实执行。

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

百度百科定义:

约定优于配置(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中如何理解约定优于配置问题

“Spring Boot中如何理解约定优于配置问题”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


网站题目:SpringBoot中如何理解约定优于配置问题
分享路径:http://kswsj.com/article/gjsgge.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号