Spring Boot Read configuration file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.