SpringBoot Getting Started (V)

A, SpringBoot of yml profile

(1) SpringBoot profile commonly used in two types, .properties and .yml.
By default, SpringBoot loaded or classpath classpath: / application.properties the config
or a pplication.yml file.

.properties file is our common configuration file, .yml is YAML file, YAML is an easy to read, easy scripting language and interactive programming language used to express the data series, here simply Syntax
  (1) Use spaces Space indent layered element layer aligned with the left, the number of different layers of different elements with spaces indentation (Tab can not be used, the number of spaces corresponding to the respective system Tab may be different, leading to confusion levels);
  (2) # denotes a comment;
  (3 ) behind a dash represents a list with space;
  (4) separated by a colon and a space between the key and value.

environments:
    dev:
        url: http://dev.bar.com
        name: Developer Setup
    prod:
        url: http://foo.bar.com
        name: My Cool App

#yaml 的list集合
my:
   servers:
       - dev.bar.com
       - foo.bar.com

The code segment is file content yml a standard equivalent to the following configuration properties

environments.dev.url=http://dev.bar.com
environments.dev.name=Developer Setup
environments.prod.url=http://foo.bar.com
environments.prod.name=My Cool App

my.servers[0]=dev.bar.com
my.servers[1]=foo.bar.com

SpringBoot by YamlPropertiesFactoryBean to load application.yml file, and by YamlMapFactoryBean convert it into the Map .
  We can by @Value or @ConfigurationProperties annotation to retrieve data in the configuration file. For example application.yml the following documents:

name:
    nameStr
age:
    11

#yaml 的list集合
my:
    servers:
       - dev.bar.com
       - foo.bar.com
@Configuration
public class MyConfiguration {
    // 通过@Value获取值
    @Value("${name}")
    private String name;

    @Value("${age}")
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
@Configuration
//通过@ConfigurationProperties获取值
@ConfigurationProperties(prefix="my")
public class MyConfig {

    private List<String> servers = new ArrayList<String>();

    public List<String> getServers() {
        return this.servers;
    }

}
@SpringBootApplication
@RestController
public class Application {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.run(args);
    }


    @Autowired
    private MyConfiguration myConfiguration;

    @RequestMapping("/getMyConfiguration")
    public String getMyConfiguration() {
        return myConfiguration.getName() + ":" + myConfiguration.getAge();
    }

    @Autowired
    private MyConfig myConfig;

    @RequestMapping("/getMyConfig")
    public List<String> getMyConfig() {
        return myConfig.getServers();
    }
}

Start Application, respectively, in the browser enter http: // localhost: 8080 / getMyConfiguration and http: // localhost: 8080 / getMyConfig can be configured value.
  When using the configuration values acquired @ConfigurationProperties, SpringBoot is also provided a loose binding, (@Value not support this feature):

@ConfigurationProperties(prefix="person")
public class OwnerProperties {

    private String firstName;

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

}

The configuration values ​​can be acquired using any of a corresponding manner as in the configuration file

person.firstName
person.first-name
person.first_name
PERSON_FIRST_NAME
Published 29 original articles · won praise 4 · Views 858

Guess you like

Origin blog.csdn.net/hukehukehukehuke/article/details/104055665