在spring boots配置中取值
2018-11-20 本文已影响0人
陈凌川
在application.yaml添加配置
server:
port: 8001
size: big
index: 1
content: "欢迎使用 ${size} 版本:${index}"
创建配置类
使用@Value关联配置中的值
package com.example.demo;
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;
/**
* Created by chenbo on 2018/5/24.
*/
@RestController
public class HelloController {
@Value ( "${size}" )
private String size;
@Value ( "${index}" )
private int index;
@Value ( "${content}" )
private String content;
@RequestMapping(value = "/hello" , method = RequestMethod.GET)
public String say(){
return "欢迎使用!"+ index;
}
}