2.重启服务
application.xml
在目录上生成日志文件
logging: file: name: log
(六)⭐YAML⭐
1.properties与yaml
SpringBoot使用一个全局的配置文件,配置文件名称是归档的
- application.properties
语法结构: key=value - application.yaml
语法结构: key:空格
value
(1).介绍yaml配置文件
YAML是"YAML Ain't a Markup Language"(YAML不是一种标记语言)的递归缩写
。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)
,但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名。
(2).标记语言:
⭐YAML A Markp Language : 是一种标记语言。
⭐YAML isnot Markp Language: 不是一个标记语言。
以前的配置文件。大多数都是使用xml来配置;比如一个简单的端口配置,我们来对比以下yaml和xml的区别。
yaml配置:
# springBoot 的配置文件 # 更改Tomcat的端口号 server: port: 8081
# springBoot 的配置文件 # 更改Tomcat的端口号 <server> <port>8081<port> <server>
2.yaml常用的基本形式
# springBoot 的配置文件 对空格的要求十分高。 # 更改Tomcat的端口号 server: port: 8081 # 普通的 Key -value name: Jsxs # 对象 student: name: jsxs age: 3 # 对象行内写法 student1: {name: jsxs,age: 2} #数组 pets: - cate - dog - pig #数组行内写法 pets1: [cat,dog,pig]
3.yaml的作用
(1).利用注解进行赋值(注解赋值)
在这里我们设置组件(@Compont)和Value(@Value)以及自动装配的(@Resource),可以帮助我们不用配置xml文件,从而实现联结
实体类 dog
package com.example.springboot01hello.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component //设置bean组件的操作 public class Dog { @Value("旺财") // 给属性进行命名的操作 value private String name; @Value("2") private Integer age; public Dog() { } public Dog(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
实体类人
package com.example.springboot01hello.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; @Component public class people { private String name; private Integer age; private Boolean happy; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; public people(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) { this.name = name; this.age = age; this.happy = happy; this.birth = birth; this.maps = maps; this.lists = lists; this.dog = dog; } public people() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getHappy() { return happy; } public void setHappy(Boolean happy) { this.happy = happy; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public Map<String, Object> getMaps() { return maps; } public void setMaps(Map<String, Object> maps) { this.maps = maps; } public List<Object> getLists() { return lists; } public void setLists(List<Object> lists) { this.lists = lists; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } @Override public String toString() { return "people{" + "name='" + name + '\'' + ", age=" + age + ", happy=" + happy + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + '}'; } }
测试类的实现
package com.example.springboot01hello; import com.example.springboot01hello.pojo.Dog; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; @SpringBootTest class SpringBoot01HelloApplicationTests { @Resource //进行自动装配的操作,也就是我们前面的 private Dog dog; @Test void contextLoads() { System.out.println(dog); } }
(2).利用yaml进行配置和读取配置属性(@ConfigurationProperties)
Data日期的格式一定要是: xxxx/xx/xx
添加如下依赖: 我们的就不会爆红了
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>2.7.7</version> </dependency>
赋值实体类中的 people类
@ConfigurationProperties作用: 将配置文件中配置的每一个属性的值,映射到这个组件中; 告诉SpringBoot将本类中的所有属性和配置文件相关的配置进行绑定 参数: prefix = "person"将配置文件中的person下面的所有书信一一对应 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能 这里面的命名不能使用驼峰,全部小写
package com.example.springboot01hello.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; @Component /*@ConfigurationProperties作用: * 将配置文件中配置的每一个属性的值,映射到这个组件中; * 告诉SpringBoot将本类中的所有属性和配置文件相关的配置进行绑定 * 参数: prefix = "person"将配置文件中的person下面的所有书信一一对应 * * 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能 */ @ConfigurationProperties(prefix = "person") //这里面的命名不能使用驼峰,全部小写 public class people { private String name; private Integer age; private Boolean happy; private Date birth; //这里的日期格式,一定要是xxxx/xx/xx否则报错 private Map<String,Object> maps; private List<Object> lists; private Dog dog; public people(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) { this.name = name; this.age = age; this.happy = happy; this.birth = birth; this.maps = maps; this.lists = lists; this.dog = dog; } public people() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getHappy() { return happy; } public void setHappy(Boolean happy) { this.happy = happy; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public Map<String, Object> getMaps() { return maps; } public void setMaps(Map<String, Object> maps) { this.maps = maps; } public List<Object> getLists() { return lists; } public void setLists(List<Object> lists) { this.lists = lists; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } @Override public String toString() { return "people{" + "name='" + name + '\'' + ", age=" + age + ", happy=" + happy + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + '}'; } }