springbootproperties.yml配置文件的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36361038/article/details/80718449

第一种配置方式:

1,在配置文件中加入下面的配置

server:
  port: 8080
  cupSize: B
 age: 20 #配置属性并给他指定的值
content: "cupSize: ${server.cupSize},age: ${server.age}"  #配置属性之后再次套用里面的属性的值

2、在创建的使用类中使用如下

第一种回去配置文件中属性的值的方法, 原始的获取属性的值的方法
 //引入配置文件里面的cupSize属性并把该属性的值注解到cupSize属性中
 @Value("${server.cupSize}")
 private  String cupSize;
 @Value("${server.age}")
 private int age;
 @Value("${content}")
 private String content;

第二种配置方式:即如何把配置写到一个类里面去

1、在配置文件中加入下面这样的方式

server:
  port: 8080
girl:
  cupSize: B
  age: 20

2、创建一个类来接受配置文件中的属性

@Component //相当于把该类注入带bean中
@ConfigurationProperties(prefix ="girl") //这个是配置文件中的配置的属性的前缀
public class GirlProperties {
    private  String cupSize;
    private  Integer age;

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }

    public Integer getAge() {
        return age;
    }

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

3、在使用类中直接注入上面创建的类并通过get和set方法直接获取到配置文件中的属性的值

@Autowired
private  GirlProperties girlProperties;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public  String say(){
    return "尺寸大小:"+girlProperties.getCupSize()+" 年龄:"+girlProperties.getAge();
}



猜你喜欢

转载自blog.csdn.net/qq_36361038/article/details/80718449