springboot reads the configuration information, environment variables

2, reads the configuration file

2.1 reads the core profile

The core configuration file is application.properties or application.yml configuration file in the root directory of resources, read the two configuration files in two ways, they are relatively simple.

Application.properties core configuration file as follows:

server.port=9090

test.msg=Hello World Springboot!

 

2.1.1 Use @Value way (common):

 

@RestController

public class WebController {

 

    @Value("${test.msg}")

    private String msg;

 

    @RequestMapping(value = "index", method = RequestMethod.GET)

    public String index() {

        return "The Way 1 : " +msg;

    }

}

 

2.1.2 Using Environment way

@RestController

public class WebController {

 

    @Autowired

    private Environment env;

 

    @RequestMapping(value = "index2", method = RequestMethod.GET)

    public String index2() {

 

        return "The Way 2 : " + env.getProperty("test.msg");

    }

}

 

2.1.3 Let springboot convert java class

@Component

@ConfigurationProperties(prefix="people")

public class People {

    private String name;

    private Integer age;

    private List<String> address;

    private Phone phone;

    public String getName() {

        return name;

}

public class Rest {

    

    @Autowired

private People me;

 

    @RequestMapping("/bbb")

    public String hello() {

        return " good " + me.getName() + me.getAge() + me.getAddress() + me.getPhone().getNumber();

    }

}

2.2 reads the custom configuration file
in order not to undermine the core files of the original ecosystem, but the need for custom configuration information exists, under normal circumstances would choose the custom configuration files to put these custom information under here in resources / config directory create a profile my-web.properties

@ConfigurationProperties(locations = "classpath:config/my-web.properties", prefix = "web")

@Component

public class MyWebConfig {

    private String name;

    private String version;

    private String author;

    public String getAuthor() {

        return author;

    }

 

2.3 reads environment variables

When the micro-service development, some configuration information (such as a database user name, password) read from the configuration file, but in paas, you need to read from the environment variable docker's.

server:

  port: 9090

people:

 

#  name: "hhh"

#  name: ${COMPUTERNAME}

  name: $ {M2_HOME: abc} # First, take the environment variable, if there is no environment variable, to take fixed value abc

  age: 38

  address:

    - 111

    - 222

  phone:

    Number The: 13,026,613,740
----------------
Disclaimer: This article is CSDN blogger "ggaofeng 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/ggaofengg/article/details/79380055

Guess you like

Origin www.cnblogs.com/moxiaotao/p/11577005.html