目录
1.系统配置文件:配置文件通常用于指定应用程序的全局设置,比如数据库连接、日志级别、端口号等。可以在配置文件中定义这些属性,并根据需要进行修改。
2.用户配置文件:通过使用Spring Boot的配置文件,可以让用户在运行时指定他们希望覆盖的属性。例如,应用程序可能具有一些默认设置,但允许用户在配置文件中指定其他值
.properties和.yml
2.0 特殊说明:
1. 当.properties中设置端口号server.port=8888,而在.yml中设置
server:
port:6666,这个时候会以8888端口号!当一个项目中存在两种格式的配置文件,并且两个配置文件中设置了相同配置项,properties的优先级会更高
2. 理论上这两种配置文件是可以共存的,但实际上我们会统一
3. properties第一次用的话使用中文会乱码,但.yml不会,但是也可以通过别的方法来改变properties中文乱码的问题,下文中有介绍
4.yml的通用性更好
#表示注释;key=value的形式
- server.port=8888
- mytest=zhangsan
- #连接数据库配置
- spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=utf8
- spring.datasource.username=root
- spring.datasource.password=123456
mytest=张三,像这种使用中文会乱码,但是你也可以通过改成utf8的形式改变这个问题。![]()
@PropertySource(value="application.properties",encoding="utf-8") 但是这种方式必须保证在application.properties , 中有![]()
- @Value(value = "${my.value}", encoding = "utf-8")
- private String myValue;
server: port: 6666 #自定义配置类 mytest2: lisi这个必须值的前面有空格,这里的mytets2= 张三,写成中文也不会乱码。
- # 配置数据库连接
- spring:
- datasource:
- url: jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=utf8
- username: root
- password: 123456
对比一下:

- # Null,~表示null,下行表示null是key,而value是一个特殊的值
- null.value: ~
- student:
- id: 1
- name: java
- age: 18
- # 或者就使用行内写法
- student2: {id: 1,name: java,age: 18}
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties("student")//将配置文件的属性值与一个特定的Java类库绑定起来 @Data //使用lombok的@Data注解,里面就是我们想要的get,set,toString等方法 public class Student { private int id; private String name; private int age; }
注意事项 :
- Student类上方有@ConfigurationProperties("student")注解,并且里面写的必须与application.yml中的自定义配置对象名字一样;
- 实体类属性名要与配置中的key保持一致,并且提供settet和getter;则会就可以使用lombok中的@Data注解
@Configuration注解就是把配置文件中的某个属性值与某个特定的Java类绑定起来。
# 配置对象 student: id: 1 name: java age: 18 # 或者就使用行内写法 student2: {id: 1,name: java,age: 18}
最后的打印结果
在 YAML 配置文件中,可以使用列表来定义一组值。
students: - id: 1 name: Alice age: 18 - id: 2 name: Bob age: 20 - id: 3 name: Charlie age: 22在上面的配置中,我们定义了一个名为 `students` 的列表,其中包含了三个学生的信息,每个学生的信息又用一个 map 来表示。
在 Spring Boot 中,可以通过 `@ConfigurationProperties` 注解来将 YAML 配置文件中的值注入到 Java 对象中。例如,我们可以创建一个 `Student` 类来表示一个学生的信息:
@Data public class Student { private int id; private String name; private int age; }然后,在 Spring Boot 应用程序中,可以使用以下方式将 `students` 列表中的所有元素注入到一个 `List
` 对象中:
@Component @ConfigurationProperties("students") @Data public class StudentConfig { private Liststudents; }在上面的代码中,我们使用 `@Component` 和 `@ConfigurationProperties` 注解将 `StudentConfig` 类声明为一个 Spring Bean,并将其与 YAML 配置文件中以 `students` 为前缀的配置项绑定起来。Spring Boot 会自动将 `students` 列表中的所有元素映射为 `List
` 对象的属性值。 现在,我们可以在其他组件中注入 `StudentConfig` 对象,并使用其中的 `students` 属性来获取所有学生的信息了。例如:
```java @Service public class StudentService { private final StudentConfig studentConfig; @Autowired public StudentService(StudentConfig studentConfig) { this.studentConfig = studentConfig; } public ListgetAllStudents() { return studentConfig.getStudents(); } } ```在上面的代码中,我们注入了 `StudentConfig` 对象,并在 `getAllStudents()` 方法中返回了其中的 `students` 属性。
这样,我们就可以轻松地将 YAML 配置文件中的列表注入到 Java 对象中,并在 Spring Boot 应用程序的其他组件中使用了。
dev开发环境中可以设置端口号:1111
prod中可以设置成别的端口号:2222
在application.yml中可以去选择使用哪一种环境
spring: profiles: active: dev