Spring Boot 配置文件二
application.properties
## 家乡属性 Dev
home.province=广东
home.city=深圳
home.desc=: 我生活在 ${home.province} ${home.city}.
属性类
package com.ghgcn.hello.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "home")
public class HomeProperties {
private String province;
private String city;
private String desc;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "HomeProperties{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
", desc='" + desc + '\'' +
'}';
}
}
controller中使用
@RestController
public class HelloController {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private HomeProperties homeProperties;
@GetMapping("/hello/home")
public HomeProperties getHome(){
logger.info(homeProperties.toString());
return homeProperties;
}
}
启动访问