Idea creates a springboot project and uses the yml configuration file to inject values into the attributes

1.idea click File in the upper left corner, select new, click project

2. Select Spring Initializr, pay attention to sdk choose your own locally installed jdk, and then click next in the lower right corner

3. Fill in according to actual needs, and then click next

.

4. Select the required dependencies of your own project according to your needs, then Next, click Finish after confirming the project path

5. After completion, pay attention to modify the configuration of maven, configure it as a locally installed maven

6. The style after the project is generated is as follows, delete the useless files marked in red

7. Create a new application.yml file, which is loaded by springboot by default

8. Fill in the information according to the actual needs of your project, pay attention to the space between the key and the value, remember, some key names may be several

A combination of words, such as robotUrl in the entity class, when defined in the configuration file, both robotUrl and robot_url can be matched

9. Create an entity class according to the needs of the project. The attributes of the entity class should match the configuration file name and generate

Get and set methods, toString methods, add @ConfigurationProperties annotations to the entity class, the role of this annotation is to explain

The attributes in the class correspond to the attributes of the configuration file one-to-one. Note that @Component annotations must be added, otherwise an error will be reported, which can also be in the pom.xml file

Import the jar package of spring-boot-configuration-processor so that when the entity class and the configuration file are bound, relevant prompts can be made

10. Use the test class junit.test to test it

11. Test results

Guess you like

Origin blog.csdn.net/qq_39839828/article/details/104951116