spring默认的属性配置文件是:application.propertity
推荐使用application.yml
server:
port: 8080
cupSize: F
girl:
cupSize: G
age: 22
GirlProperty.java
package com.rick;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Created by rick on 2017/1/10.
*/
@ConfigurationProperties(prefix = "girl")
@Component
public class GirlProperty {
private Integer age;
private String cupSize;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
}
属性使用HelloController.java
package com.rick;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* Created by rick on 2017/1/10.
*/
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@Resource
private GirlProperty girlProperty;
@RequestMapping(value="/hello", method = RequestMethod.GET)
public String sayHello() {
return girlProperty.getCupSize() + "=>" + girlProperty.getAge();
}