This article from the "Knowledge Forest"
Read the core configuration file
The core configuration file refers to the resources root directory application.properties
or application.yml
configuration file, read the two configuration files in two ways, are relatively simple.
The contents of the core configuration file application.properties
are as follows:
server.port=9090test.msg=Hello World Springboot!
- How to use
@Value
(common):
@RestControllerpublic class WebController { @Value("${test.msg}") private String msg; @RequestMapping(value = "index", method = RequestMethod.GET) public String index() { return "The Way 1 : " +msg; }}
Note: the @Value
key name in the core configuration file is included in the ${}. Adding to the controller class @RestController
means that all views in this class are displayed in JSON, similar to adding on a view method @ResponseBody
.
Access: Http://localhost:9090/index will getThe Way 1 : Hello World Springboot!
@RestControllerpublic class WebController { @Autowired private Environment env; @RequestMapping(value = "index2", method = RequestMethod.GET) public String index2() { return "The Way 2 : " + env.getProperty("test.msg"); }}
Note: This is done by relying on injection, Evnironment
adding annotations on the created member variable to private Environment env
complete the @Autowired
dependency injection, and then using it to env.getProperty("键名")
read the corresponding value.
Access: HTTP://LOCALHOST:9090/INDEX2 will getThe Way 2 : Hello World Springboot!
To read a custom configuration file
In order not to destroy the original ecology of the core file, but also need to have the custom configuration information exists, in general will choose the custom configuration file to put these custom information, here in the resources/config
directory to create the configuration filemy-web.properties
resources/config/my-web.properties
The contents are as follows:
1.0web.author=[email protected]
To create an entity class that manages the configuration:
@ConfigurationProperties (locations ="Classpath:config/my-web.properties", prefix ="Web")@ComponentPublicClassMywebconfig {private String name;Private String version;Private String author;Public Stringgetauthor () {return author;} public String getname () {return Name } public String getversion () { return version; } public void setAuthor (String Author) {this.author = author;} public void setName (String name) { Span class= "Hljs-keyword" >this.name = name; } public void setVersion (String Version) {this.version = version;}}
Attention:
@ConfigurationProperties
There are two properties in the note:
locations
: Specify the location of the configuration file
prefix
: Specifies the prefix of the key name in the configuration file (I have all the key names in the configuration file to web.
begin with)
Use @Component
is to allow the class to be used elsewhere, using @Autowired
annotations to create instances.
Create a test controller
@RestController @RequestMapping (value = " config ") public Span class= "Hljs-class" >class configcontroller { @Autowired private mywebconfig mywebconfig; @RequestMapping (value = "index", method = Requestmethod.get ) public String index () {return Span class= "hljs-string" > "WebName:" +mywebconfig.getname () + "webversion:" + Mywebconfig.getversion () +
Note: because annotations are added to the Mywebconfig class @Component
, you can use it here directly @Autowired
to create its instance object.
Access: Http://localhost:9090/config/index will getwebName: zslin, webVersion: V 1.0, webAuthor: [email protected]
Example code: HTTPS://GITHUB.COM/ZSL131/SPRING-BOOT-TEST/TREE/MASTER/STUDY02
This article from the "Knowledge Forest"
Springboot custom profiles and read configuration files