springboot中的基本配置(yml)
1.yml数据格式
1.1普通格式
country: China
province: SuZhou
city: SuZhou
area: gusu
person:
name: Li
age: 24
1.2数组格式
完整写法
hobby:
- 跳舞
- 篮球
- rap
缩略写法
likes: [篮球,足球,羽毛球,网球]
1.3对象数组格式
完整写法
users:
- name: Tom
age: 43
- name: Jerry
age: 24
缩略写法
users1: [{name:Tom,age:43},{name:Jerry,age:24}]
2.获取yml属性数据
yml中数据展示
person:
name: Li
age: 24
country: China
province: SuZhou
city: SuZhou
area: gusu
hobby:
- 跳舞
- 篮球
- rap
users:
- name: Tom
age: 43
- name: Jerry
age: 24
#使用${属性名}引用数据
beforeDir: c:\windows
newDir: ${beforeDir}\new
方法一:@Value注解方法(单一数据)
@RestController
@RequestMapping("hello")
public class HelloController {
@Value("${person.name}")
private String RoleName;
@Value("${country}")
private String RoleCountry;
@Value("${hobby[2]}")
private String hobbyRap;
@Value("${users[0].name}")
private String usersName1;
@Value("${newDir}")
private String newDir;
@GetMapping
public String hello(){
System.out.println("RoleName=>"+RoleName);
System.out.println("RoleCountry=>"+RoleCountry);
System.out.println("hobbyRap=>"+hobbyRap);
System.out.println("usersName1=>"+usersName1);
System.out.println("newDir=>"+newDir);
return "hello~,Nice to meet you! Darling";
}
}
方法二:Environment对象(全部数据)
SpringBoot可以使用@Autowired,自动装配,将所有的数据封装到一个对象Environment中,通过Environment中的getProperty(String name)方法获取数据。
@RestController
@RequestMapping("hello")
public class HelloController {
//自动装配将所有的数据封装到一个对象Environment中
@Autowired
private Environment env;
@GetMapping
public String hello(){
System.out.println(env.getProperty("person.name"));
System.out.println(env.getProperty("country"));
System.out.println(env.getProperty("hobby[2]"));
System.out.println(env.getProperty("users[0].name"));
System.out.println(env.getProperty("newDir"));
return "hello~,Nice to meet you! Darling";
}
}
方法三:自定义数据类型(常用)
数据准备
datasouce:
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/springweb
username: root
password: master
基本步骤
1.创建类,用于封装数据。
2.将实体类bean的创建交给Spring管理
@Component 定义为Spring管控的bean
@ConfigurationProperties(prefix="xxx") 指定加载数据
3.使用时从Spring中直接获取信息
实际操作
构建MyData类
@Component
@ConfigurationProperties(prefix = "datasouce")
public class MyData {
private String driver;
private String url;
private String username;
private String password;
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "MyData{" +
"driver='" + driver + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
在HelloController中进行注入
@RestController
@RequestMapping("hello")
public class HelloController {
@Autowired
private MyData data;
@GetMapping
public String hello(){
System.out.println(data);
return "hello~,Nice to meet you! Darling";
}
}

浙公网安备 33010602011771号