SpringBoot in yml profile

1, yml profile writing format

  It is a common configuration file format in the attribute name to split, which is ".", ":" And a line feed.

  example:

// general format 
spring.datasource.driver- class -name = com.mysql.jdbc.Driver

// YML format 
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver

  Note :

    1, annotated format in the configuration file is

      #annotation

    2, in the spring is the difference between the two letters dataSource.

    3, attributes and values ​​are between a colon and a space is not written directly after the colon.

      

2, taken in the common key controller layer

  In @Value annotations ( "$ {name} properties") to the value.

  controller layer value will normally be assigned to the property.

@Value("${offcn_ip}")
private String port;

@RequestMapping("/one")
public String getOne(){
return port;
}

3, the object takes pojo

  1, a writing object configuration file pojo

user:
  username: zhangsan
  age: 23
  id: 1

  2, the preparation of an entity class

  There must be @ConfigurationProperties this comment in the entity class, and specify prrfix prefix.

@ConfigurationProperties(prefix = "user")
public class User {
    private String username;
    private Integer age;
    private Integer id;

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + username + '\'' +
                ", age=" + age +
                ", id=" + id +
                '}';
    }
}

3, using

@RestController
@EnableConfigurationProperties({User.class})
public class Yml {
  @Autowired
    User user;
    @RequestMapping("/one")
    public String getOne(){
        return user.toString();
    }
}

EnableConfigurationProperties comment on the call need to add the class, or add the startup class SpringbootSimpleApplication on the can.

This is a simple configuration file for yml what's called, you will be able to get data access request path.

Guess you like

Origin www.cnblogs.com/xueziyeya/p/11808135.html