Java boot assembles a set of yml configuration information into an object

In fact, encapsulating a set of yml data into an object is the mainstream development method in the future

We create a springboot project and find the directory where the startup class in the project is located.
Create a class name in the same directory. You can take whatever I want here. I call it dataManager
insert image description here
and define such a set of data information in yml
insert image description here
. Then we define three and this in the class. Field variables with the same configuration information are connected.
If they are not correct, something will happen.
Then define their get and set methods
insert image description here
. Then we add two annotations to the head of the dataManager.

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("systemuser")

You must still use Component to manage the current class to spring to read it,
and then let him read the configuration in yml through ConfigurationProperties,
and the value in brackets we want to read which configuration to write in, otherwise how does the system know what you want to read? Here to read systemuser

In this way, as long as the attribute names in your class and the field names in yml can be matched

Well, let's test it and directly introduce the class dataManager we wrote in the place where it can run,
and then use the Autowired annotation to automatically assemble it
insert image description here
, and then we run the interface to output the name attribute

insert image description here
This geitName outputs this content

This obviously read the content name in yml
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/131090946