springboot属性配置

来源:互联网 发布:javascript 跳转页面 编辑:程序博客网 时间:2024/05/16 06:06

1.引入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>

2.配置文件(application.yml)

   要注意属性名冒号后与值要有一个空格

girl:    age: 100    size: hello3.创建一个属性类(GirlProperties
package com.example.Properties;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * Created by wangjiwei on 2017/5/3. */@Component@ConfigurationProperties(prefix = "girl")public class GirlProperties {    private Integer age;    public String getSize() {        return size;    }    public void setSize(String size) {        this.size = size;    }    private String size;    public GirlProperties() {        super();    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }}
)
测试
package com.example.controller;import com.example.Properties.GirlProperties;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Created by wangjiwei on 2017/5/2. */@RestControllerpublic class HelloController {    @Autowired   private GirlProperties girlProperties;    @RequestMapping(value = "/*")    public String sayHello(){     return girlProperties.getSize();   }}



 

1 0