One, springboot configuration file
Core configuration files and custom profiles. A core profile is a configuration file in the resources root application.properties
directory application.yml
. In order not to destroy the original ecology of the core file, but also need to have custom configuration information exists, in general will choose a custom configuration file to put these custom
Information, here in the resources/config
directory to create the configuration file config.properties
Two ways to read the core configuration file
Core configuration file Contents:
Server: port:8081 context-path:/bootservice: Name:freya
(1) @Value ("${Key Name}")
Restcontrollerpublic class Hellocontroller { @Value ("${service.name}") private String name; @RequestMapping (value = "/hello") public String Hello () { return "Hello Spring boot!" + name; }}
(2) using Environment,env.getproperty ("Key Name")
@RestControllerpublic class Hellocontroller { @Autowired private environment env; @RequestMapping (value = "/hello") public String Hello () { return "Hello Spring boot!" + env.getproperty ("Servic E.name ");} }
Iii. How to read a custom configuration file
The contents of the custom config.properties are as follows:
Project.version=1.0-snapshotproject.name=boot
Create an entity class Myconfig.
Note: The springboot1.5 version below @configurationproperties has two properties locations (Specify the location of the configuration file),
Prefix (Specifies the prefix of the key name in the configuration file). However, version 1.5 and above cancels the locations attribute in order to specify the configuration file's
Location, use @propertysource (value = "Custom profile path") to specify where the file is located.
@Component @configurationproperties (prefix = "project") @PropertySource (value = "Classpath:config/config.properties") public class Myconfig { private String version; private String name; Public String getversion () { return version; } public void Setversion (String version) { this.version = version; } Public String GetName () { return name; } public void SetName (String name) { this.name = name; }}
@RestControllerpublic class Hellocontroller { @Autowired private myconfig myconfig; @RequestMapping (value = "/hello") public String Hello () { return "Hello Spring boot!" myconfig.getversion () + "" + myconfig.getname (); }}
Spring Boot Read configuration file