文章目录

  • ​​一. 属性配置​​
  • ​​1. 修改服务器端口​​
  • ​​2. 修改配置​​
  • ​​3. SpringBoot内置属性查询​​
  • ​​二. 配置文件分类​​
  • ​​三. yaml文件​​
  • ​​1. yaml语法规则​​
  • ​​2. yaml数据读取​​
  • ​​2.1 读取单个数据​​
  • ​​2.2 读取全部数据​​
  • ​​2.3 自定义对象封装指定数据​​

一. 属性配置

1. 修改服务器端口

SpringBoot——springboot配置_数据

2. 修改配置

SpringBoot——springboot配置_spring_02

# 服务器端口配置
server.port=80

# 修改banner
# spring.main.banner-mode=off 关闭
# spring.banner.image.location=logo.png

# 控制日志
logging.level.root=info
# logging.level.com.itheima=warn

3. SpringBoot内置属性查询

二. 配置文件分类

  • properties(传统格式/默认格式)
  • yml(主流格式)
  • yaml

SpringBoot——springboot配置_System_03


SpringBoot——springboot配置_spring boot_04

不同配置文件中相同配置按照加载优先级相互覆盖,不同配置文件中不同配置全部保留

SpringBoot——springboot配置_java_05

三. yaml文件

SpringBoot——springboot配置_spring_06

1. yaml语法规则

SpringBoot——springboot配置_spring_07

package com.itheima.controller;

import com.itheima.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//Rest模式
@RestController
@RequestMapping("/books")
public class BookController {

//读取yaml数据中的单一数据
@Value("${country}")
private String country1;

@Value("${user.name}")
private String name1;

@Value("${likes[1]}")
private String likes1;

@Value("${users[1].age}")
private String age1;

@Value("${server.port}")
private String port;

@Value("${tempDir}")
private String tempDir;

//使用自动装配将所有的数据封装到一个对象Environment中
//下面就可以env直接获取数据,不需要再单独定义变量了
@Autowired
private Environment env;

@Autowired
private MyDataSource myDataSource;

@GetMapping
public String getById(){
System.out.println("springboot is running...");
System.out.println("country1===>"+country1);
System.out.println("name1===>"+name1);
System.out.println("likes1===>"+likes1);
System.out.println("age1===>"+age1);
System.out.println("port===>"+port);
System.out.println("tempDir===>"+tempDir);
System.out.println("-----------------------------");
System.out.println(env.getProperty("server.port"));
System.out.println(env.getProperty("user.name"));
System.out.println("-----------------------------");
System.out.println(myDataSource);
return "springboot is running...";
}

}
country: china
province: beijing
city: beijing
area: haidian

port: 8080

party: true

birthday: 1949-10-01

user:
name: itcast
age: 16

user2:
name: itheima
age: 16


a:
b:
c:
d:
e: 123


likes:
- game
- music
- sleep

# 缩略版
likes2: [game,music,sleep]

users:
- name: zhangsan
age: 18
- name: lisi
age: 17

# 缩略版
users3: [{name:zhangsan,age:18},{name:lisi,age:17}]

users2:
-
name: zhangsan
age: 18
-
name: lisi
age: 17



baseDir: c:\win10

# 使用${属性名}引用数据
tempDir: ${baseDir}\temp
# 使用引号包裹的字符串,其中转义字符可以生效
tempDir2: "${baseDir}\temp \t1 \t2 \t3"

# 创建类,用于封装下面的数据
# 由spring帮我们去加载数据到对象中,一定要告诉spring加载这组信息
# 使用时候从spring中直接获取信息使用
datasource:
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/test
username: root
password: root

server:
port: 80
servlet:
context-path: /test123

SpringBoot——springboot配置_System_08

package com.itheima;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

//1.定义数据模型封装yaml文件中对应的数据
//2.定义为spring管控的bean
@Component
//3.指定加载的数据
@ConfigurationProperties(prefix = "datasource")
public class MyDataSource {
private String driver;
private String url;
private String username;
private String password;

@Override
public String toString() {
return "MyDataSource{" +
"driver='" + driver + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + 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;
}
}

SpringBoot——springboot配置_spring boot_09

SpringBoot——springboot配置_spring_10


字面值表示方式

SpringBoot——springboot配置_spring_11


数组表示方式:在属性名书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔

SpringBoot——springboot配置_java_12

2. yaml数据读取

2.1 读取单个数据

SpringBoot——springboot配置_数据_13


SpringBoot——springboot配置_数据_14

2.2 读取全部数据

SpringBoot——springboot配置_数据_15

  • 使用Environment对象封装全部配置信息
  • 使用@Autowired自动装配数据到Environment对象中

2.3 自定义对象封装指定数据

SpringBoot——springboot配置_数据_16

SpringBoot——springboot配置_java_17

  • 使用@ConfigurationProperties注解绑定配置信息到封装类中
  • 封装类需要定义为Spring管理的bean,否则无法进行属性注入