vs2015 做网站,区域门户网站源码,建设部人事教育司网站,怎么制作网站视频目录 1. Spring Boot 配置文件的使用场景 2. 配置文件的两种格式 2.0 特殊说明#xff1a; 2.1 .properties 2.1.1 格式 2.2.2 缺陷 2.2.3 解决中文乱码的问题 2.2 .yml 2.2.3 格式 配置数据库连接 注意转义字符 编辑 编辑 配置null 配置对象 从.yml读取文件举例 Stud… 目录 1. Spring Boot 配置文件的使用场景 2. 配置文件的两种格式 2.0 特殊说明 2.1 .properties 2.1.1 格式 2.2.2 缺陷 2.2.3 解决中文乱码的问题 2.2 .yml 2.2.3 格式 配置数据库连接 注意转义字符 编辑 编辑 配置null 配置对象 从.yml读取文件举例 Student类及注意事项 appliaction.yml Test 配置集合示例 创建不同环境下的配置文件 1. Spring Boot 配置文件的使用场景 1.系统配置文件配置文件通常用于指定应用程序的全局设置比如数据库连接、日志级别、端口号等。可以在配置文件中定义这些属性并根据需要进行修改。 2.用户配置文件通过使用Spring Boot的配置文件可以让用户在运行时指定他们希望覆盖的属性。例如应用程序可能具有一些默认设置但允许用户在配置文件中指定其他值 2. 配置文件的两种格式 .properties和.yml 2.0 特殊说明 1. 当.properties中设置端口号server.port8888,而在.yml中设置 server: port:6666,这个时候会以8888端口号当一个项目中存在两种格式的配置文件并且两个配置文件中设置了相同配置项properties的优先级会更高 2. 理论上这两种配置文件是可以共存的但实际上我们会统一 3. properties第一次用的话使用中文会乱码但.yml不会但是也可以通过别的方法来改变properties中文乱码的问题下文中有介绍 4.yml的通用性更好 2.1 .properties
2.1.1 格式
#表示注释keyvalue的形式 server.port8888
mytestzhangsan
#连接数据库配置
spring.datasource.urljdbc:mysql://127.0.0.1:3306/testdb?characterEncodingutf8
spring.datasource.usernameroot
spring.datasource.password1234562.2.2 缺陷
mytest张三,像这种使用中文会乱码但是你也可以通过改成utf8的形式改变这个问题。 2.2.3 解决中文乱码的问题
PropertySource PropertySource(valueapplication.properties,encodingutf-8) 但是这种方式必须保证在application.properties , 中有 Value(value ${my.value}, encoding utf-8)
private String myValue;2.2 .yml
2.2.3 格式 server:port: 6666
#自定义配置类
mytest2: lisi 这个必须值的前面有空格,这里的mytets2 张三写成中文也不会乱码。 配置数据库连接
# 配置数据库连接
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/testdb?characterEncodingutf8username: rootpassword: 123456 对比一下
注意转义字符 配置null
# Null,~表示null下行表示null是key而value是一个特殊的值
null.value: ~
配置对象
student:id: 1name: javaage: 18
# 或者就使用行内写法
student2: {id: 1,name: java,age: 18} 从.yml读取文件举例
Student类及注意事项 import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;Component
ConfigurationProperties(student)//将配置文件的属性值与一个特定的Java类库绑定起来
Data
//使用lombok的Data注解里面就是我们想要的getsettoString等方法
public class Student {private int id;private String name;private int age;
} 注意事项 Student类上方有ConfigurationProperties(student)注解,并且里面写的必须与application.yml中的自定义配置对象名字一样实体类属性名要与配置中的key保持一致并且提供settet和getter;则会就可以使用lombok中的Data注解 Configuration注解就是把配置文件中的某个属性值与某个特定的Java类绑定起来。 appliaction.yml # 配置对象
student:id: 1name: javaage: 18
# 或者就使用行内写法
student2: {id: 1,name: java,age: 18} Test 最后的打印结果 配置集合示例 在 YAML 配置文件中可以使用列表来定义一组值。
students:- id: 1name: Aliceage: 18- id: 2name: Bobage: 20- id: 3name: Charlieage: 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 列表中的所有元素注入到一个 ListStudent 对象中
Component
ConfigurationProperties(students)
Data
public class StudentConfig {private ListStudent students;
}在上面的代码中我们使用 Component 和 ConfigurationProperties 注解将 StudentConfig 类声明为一个 Spring Bean并将其与 YAML 配置文件中以 students 为前缀的配置项绑定起来。Spring Boot 会自动将 students 列表中的所有元素映射为 ListStudent 对象的属性值。 现在我们可以在其他组件中注入 StudentConfig 对象并使用其中的 students 属性来获取所有学生的信息了。例如 java
Service
public class StudentService {private final StudentConfig studentConfig;Autowiredpublic StudentService(StudentConfig studentConfig) {this.studentConfig studentConfig;}public ListStudent getAllStudents() {return studentConfig.getStudents();}
}在上面的代码中我们注入了 StudentConfig 对象并在 getAllStudents() 方法中返回了其中的 students 属性。 这样我们就可以轻松地将 YAML 配置文件中的列表注入到 Java 对象中并在 Spring Boot 应用程序的其他组件中使用了。 创建不同环境下的配置文件 dev开发环境中可以设置端口号1111 prod中可以设置成别的端口号2222 在application.yml中可以去选择使用哪一种环境 spring:profiles:active: dev