一、创建application.yml文件
my:
name: hundsun
age: 12
number: ${random.int}
uuid: ${random.uuid}
max: ${random.int(10)}
value: ${random.value}
greeting: hi,I'm ${my.name}
二、创建ConfigBean类
package com.hundsun.springcloud;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Description:
* @author:zhangys
* @date:Created in 10:58 2019/6/5
* @Modified By:
*/
@ConfigurationProperties(prefix = "my")
@Component
public class ConfigBean {
private String name;
private int age;
private int number;
private String uuid;
private int max;
private String value;
private String greeting;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getGreeting() {
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
@Override
public String toString() {
return "ConfigBean{" +
"name='" + name + '\'' +
", age=" + age +
", number=" + number +
", uuid='" + uuid + '\'' +
", max=" + max +
", value='" + value + '\'' +
", greeting='" + greeting + '\'' +
'}';
}
}
三、创建ConfigBeanController
package com.hundsun.springcloud.resources;
import com.hundsun.springcloud.ConfigBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description:
* @author:zhangys
* @date:Created in 11:00 2019/6/5
* @Modified By:
*/
@RestController
@RequestMapping("/configbean")
@EnableConfigurationProperties({ConfigBean.class})
public class ConfigBeanController {
@Autowired
ConfigBean configBean ;
@RequestMapping("/getconfigbean")
public String getConfigBean(){
return configBean.toString();
}
}
四、测试如下:
浏览器输入:http://localhost:8080/configbean/getconfigbean