Spring
SpringBoot VS Servlet
How Spring reads configuration files
1 @Value annotation to obtain a single configuration item
such as defining a qq music token in yml; then output, as follows:
2 Reading for objects: @ConfigurationProperties
writes a Student object in yml, then reads and outputs:
3 Reading for collections: @ConfigurationProperties
operations for collections are consistent with the above:
Note: The @Component annotation is for directly mapping the configuration file to the current class property when Spring starts.
The difference between yml and properties
- Compared with properties, yml has a simpler syntax and better cross-language versatility. It not only supports java language, but also supports golang and python;
- yml is a key: value structure, and supports more data types;
- These two kinds of files exist in a project at the same time, but the configuration items will be properties-based, and the yml file will be ignored;
- The yml writing method is error-prone, often caused by a space; although the properties writing method is more traditional and complicated, it is not easy to make mistakes.