Spring Series (6) --- Comparison of SpringBoot and Servlet and the way Spring reads configuration files

SpringBoot VS Servlet


insert image description here

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:
insert image description here


2 Reading for objects: @ConfigurationProperties
writes a Student object in yml, then reads and outputs:

insert image description here


3 Reading for collections: @ConfigurationProperties
operations for collections are consistent with the above:

insert image description here
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.

Guess you like

Origin blog.csdn.net/Onion_521257/article/details/129850810