SpringBoot属性配置-第三章

1.application.yml配置
#自定义参数对象
book:
name: A
id: 1
page: 100

2.创建实体类:
/**
* @Auther: youqc
* @Date: 2018/4/7 16:36
* @Description:
*/
@Component
@ConfigurationProperties(prefix = "book")
public class Book {
private Integer id;
private String name;
private Integer page;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getPage() {
return page;
}

public void setPage(Integer page) {
this.page = page;
}
}

3.创建Controller类
/**
* @Auther: youqc
* @Date: 2018/4/7 16:38
* @Description:
*/
@RestController
public class BookController {

@Autowired Book book;
@RequestMapping(value = "/getInfo",method = RequestMethod.GET)
public String getBookInfo(){
return "bookInfo: id="+book.getId()+",name="+book.getName()+",Page="+book.getPage();
}
}
访问结果:


原文地址:https://www.cnblogs.com/youqc/p/8733761.html