SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的
语法结构 : key=value
语法结构 :key:空格 value
- server:
- port: 8081
配置文件的作用 :修改SpringBoot自动配置的默认值,因为SpringBoot在底层都给我们自动配置好了
xml
-
8081
yaml
- server:
- port: 8081
说明:语法要求严格!
对象、Map(键值对)
- #对象、Map格式
- k:
- v1:
- v2:
- student:
- name: qinjiang
- age: 3
行内写法
student: {name: qinjiang,age: 3}
数组( List、set )
用 - 值表示数组中的一个元素,比如:
- pets:
- - cat
- - dog
- - pig
行内写法
pets: [cat,dog,pig]
yaml文件更强大的地方在于,他可以给我们的实体类直接注入匹配值!
易错点:
必须在主启动类的同级目录下建包(如:pojo),因为idea只会扫描他的同级目录!
dog类
- package com.kuang.springboot01.pojo;
-
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
-
- @Component //@Component注解作用于类,可以把类自动装配到Spring容器中
- public class Dog {
- @Value("旺财")
- private String name;
- @Value("3")
- 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 +
- '}';
- }
- }
上面是旧的赋值方式!
注意:一个@Autowired下面只能对应一个注入属性
如果注入多个属性,要写多个@Autowired!
在编写 application.properties/yaml 配置文件时,由于要配置的 Person 对象属性是我们自定义的,SprimgBoot无法自动识别,所以不会有任何书写提示!在实际开发中,为了出现代码提示的效果来方便配直。在使用@ConfigurationProperties 注解进行配置文件属性值注入时,可以在pom.xml文件中添加一个Spring Boot 提供的配置处理器依赖,示例代码如下。
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-configuration-processorartifactId>
- <optional>trueoptional>
- dependency>
在pom.xml中添加上述配置依赖后,还需要重新运行项目启动类或者使用“Ctit+F9”组合键(即 Buildoject)重构当前Spring Boot项目方可生效!!!
- package com.kuang.springboot01.pojo;
-
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
-
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- import java.util.Objects;
- @Component
- @ConfigurationProperties(prefix = "person")
- public class Person {
- private String name;
- private Integer age;
- private Boolean happy;
- private Date birth;
- private Map
maps; - private List
- private Dog dog;
-
- public Person() {
- }
-
- public Person(String name, Integer age, Boolean happy, Date birth, Map
maps, List { - this.name = name;
- this.age = age;
- this.happy = happy;
- this.birth = birth;
- this.maps = maps;
- this.lists = lists;
- this.dog = dog;
- }
-
- 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
getMaps() { - return maps;
- }
-
- public void setMaps(Map
maps) { - this.maps = maps;
- }
-
- public List
- return lists;
- }
-
- public void setLists(List {
- this.lists = lists;
- }
-
- public Dog getDog() {
- return dog;
- }
-
- public void setDog(Dog dog) {
- this.dog = dog;
- }
-
- @Override
- public String toString() {
- return "Person{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", happy=" + happy +
- ", birth=" + birth +
- ", maps=" + maps +
- ", lists=" + lists +
- ", dog=" + dog +
- '}';
- }
- }
- Person:
- name: hjm
- age: 18
- happy: true
- birth: 2023/10/18
- lists:
- - code
- - music
- - girl
- dog:
- name: 旺财
- age: 3
- maps:
- k1: v1
- k2: v2
- package com.kuang.springboot01;
-
- import com.kuang.springboot01.pojo.Dog;
- import com.kuang.springboot01.pojo.Person;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- @SpringBootTest
- class SpringBoot01ApplicationTests {
- @Autowired
- private Person person;
- @Autowired
- private Dog dog;
- @Test
- void contextLoads() {
- System.out.println(person);
- System.out.println(dog);
- }
-
- }
运行结果
- Person{name='hjm', age=18, happy=true, birth=Wed Oct 18 00:00:00 CST 2023, maps={k1=v1, k2=v2}, lists=[code, music, girl], dog=Dog{name='旺财', age=3}}
- Dog{name='旺财', age=3}
所以我非常推荐使用yaml来进行配置!!!
注意点:
@configurationProperties: | @Value | |
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定( 松散语法 ) | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |