In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to understand convention over configuration in Spring Boot". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
So how do you understand that convention is better than configuration?
Baidu encyclopedia definition:
Convention over configuration (convention over configuration), also known as programming by contract, is a software design paradigm designed to reduce the number of decisions that software developers need to make and gain simple benefits without losing flexibility.
In summary, there are two points:
1. Agree on some recommended default configurations
2. Developers only need to specify the parts that do not conform to the agreement.
The advantage of this is that if the agreed default configuration meets our requirements, it can be omitted, and vice versa.
From the default configuration file (application.properties/yml) provided in Spring Boot to the default automatic configuration, you can see the convenience of the convention and a lot of configuration savings.
Let's take a look at an example of auto-configured source code in 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..}
This is an automatic configuration class for file upload, which specifies:
1. It is agreed that the configuration parameters start with the spring.servlet.multipart prefix.
2. Many default configurations have been agreed, such as 1m default upload file size.
3. It is stipulated that all parameter configuration class names are * Properties.
4. It is agreed that all automatic configuration class names are * AutoConfiguration.
5. It is agreed that all automatic configuration classes are configured in: / META-INF/spring.factories
Wait a minute...
In this way, we hardly need to write any configuration for a file upload operation, unless it can not meet the requirements, such as: now the file upload 1m is too small, add a line of custom configuration, we can also write other automatic configurations as agreed.
If you can't understand it, let's take a look at how Maven does it. Maven simply embodies the idea that the convention is greater than the configuration incisively and vividly.
That's all for "how to understand convention over configuration issues in Spring Boot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.