一、项目说明
项目环境:jdk1.8+tomcat8+idea2018
源代码github地址:
实现目标:springBoot中对于项目的依赖,插件等都采用了自动化配置,但是在实际生产中难免会手动配置一些内容。在使用spring构建项目时一般使用.xml/.properties文件保存配置,在springBoot构建的项目中也可以使用.properties作为配置文件或者使用.yaml。这里通过.yaml的方式作为配置文件,了解其基本的配置与使用。
二、配置说明
(1).yaml文件概述
yaml也是一种专门用来编写配置文件的语言,与properties作用类似,yaml利用缩进表示层级关系且区分大小写。
(2)配置文件存放的位置
注:配置文件可以放在如下四个位置,四个位置的加载顺序与标注的序号一致。
(3)代码说明
A:application.yaml
user: #d配置对象
username: 张三
usersex: 男
userage: 18
useraddress: 中国
interest: #配置list
- 读书
- 写字
- 画画
color: red,white,orange #配置数组
dress: #配置map
shoes: 耐克
jack: 七匹狼
colleague: #配置list<map>
- name: 同事一
sex: 男
- name: 同事二
sex: 女
B:User映射实体类
@Component:实体类注解
@ConfigurationProperties(prefix = "user"):配置从yaml文件中读取以user为前缀的数据
package com.example.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "user")
public class User {
private String userName;
private Integer userAge;
private String userAddress;
private String userSex;
private List<String> interest;
private String[] color;
private Map<String,String> dress;
private List<Map<String,String>> colleague;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getUserAge() {
return userAge;
}
public void setUserAge(Integer userAge) {
this.userAge = userAge;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
public List<String> getInterest() {
return interest;
}
public void setInterest(List<String> interest) {
this.interest = interest;
}
public String[] getColor() {
return color;
}
public void setColor(String[] color) {
this.color = color;
}
public Map<String, String> getDress() {
return dress;
}
public void setDress(Map<String, String> dress) {
this.dress = dress;
}
public List<Map<String, String>> getColleague() {
return colleague;
}
public void setColleague(List<Map<String, String>> colleague) {
this.colleague = colleague;
}
public User(String userName, Integer userAge, String userAddress, String userSex, List<String> interest, String[] color, Map<String, String> dress, List<Map<String, String>> colleague) {
this.userName = userName;
this.userAge = userAge;
this.userAddress = userAddress;
this.userSex = userSex;
this.interest = interest;
this.color = color;
this.dress = dress;
this.colleague = colleague;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", userAge=" + userAge +
", userAddress='" + userAddress + '\'' +
", userSex='" + userSex + '\'' +
", interest=" + interest +
", color=" + Arrays.toString(color) +
", dress=" + dress +
", colleague=" + colleague +
'}';
}
}
C:HelloSpringbootController
package com.example.controller;
import com.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloSpringbootController {
@Autowired User user;
@GetMapping("/springBoot")
public String sayHello(){
return "Hello Springboot!";
}
@GetMapping("/getUser")
public String getUser(){
return user.toString();
}
}
(4)启动项目,并访问