springboot获取配置文件中的内容

代码:

GrilApplication.java
1 @SpringBootApplication
2 public class GrilApplication {
3 
4     public static void main(String[] args) {
5         SpringApplication.run(GrilApplication.class, args);
6     }
7 }

application.yml

1 server:
2   port: 8089
3 gril:
4   cupSize: B
5   age: 18
GrilProperties.java
 1 @Component
 2 @ConfigurationProperties(prefix = "gril")
 3 public class GrilProperties {
 4     private String cupSize;
 5     private Integer age;
 6 
 7     public String getCupSize() {
 8         return cupSize;
 9     }
10 
11     public void setCupSize(String cupSize) {
12         this.cupSize = cupSize;
13     }
14 
15     public Integer getAge() {
16         return age;
17     }
18 
19     public void setAge(Integer age) {
20         this.age = age;
21     }
22 }
HelloController.java

@Controller
public class HelloController {

    @Autowired
    private GrilProperties grilProperties;
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public  String say(){

        return grilProperties.getCupSize();
    }
}

访问地址http://127.0.0.1:8089/hello

 

转载于:https://www.cnblogs.com/renshengruozhiruchujian/p/7804014.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值