springboot教程 yml配置文件 与 获取配置文件中的内容


springboot使用一个全局的配置文件, application.properties或者 application.yml,配置文件放在 ‘src/main/resources目录’,或者 ‘类路径/config’ 下。 我们可以通过全局配置文件对一些springboot的默认配置值进行修改。

一、 yml基本语法

# 表示注释,从这个字符一直到行尾,都会被解析器忽略。

基本语法规则

  • 属性和值,大小写敏感。
  • key: value形式表示一对键值对。(:冒号后必须有空格)
  • 以缩进来控制层级关系。所禁用space键,而不是tab键。
  • 缩进的空格数据不重要,相同层级的数据,必须左对齐。

支持的数据类型

  • 字面量(数字,布尔值,字符串,日期,null)

  • 对象、Map

  • List、数组


二、yml值的写法

整数

age: 2

浮点数

price: 3.14

布尔值

isOk: true

日期

birth: 2018/12/12

时间

date: 2019/11/11 21:59:43

字符串

#会对转义字符转义的形式
greet: 'hello \n world'
  
#不会对字符串字符转义的形式
greet: 'hello \n world'

对象或者Map

(1) 用换行缩进的形式表示对象与成员属性的关系。

```java test: age: 2 price: 3.14 isOk: true birth: 2018/12/12 date: 2019/11/11 21:59:43 greet: 'hello \n world' greet1: 'hello\n world' ```

(2)一行形式表示对象与成员属性的关系

```java maps: { k1: v1, k2: v2 } ```

数组,List,Set
用(-)符号表示数组的元素

pets:
	- cat  #(-)和value之间至少有一个空格。
	- dog
	- pig

三、@ConfigurationProperties注解获取全局配置文件中内容

yml中配置复杂对象

person:
	lastName: zhangsan
	age: 18
	boss: false
	birth: 2017/12/12
	maps: { k1: v1, k2: v2 }
	list:
		- lisi
		- wangwu
	dog:
		name: 小狗
		age: 2

在java类中读取全局配置文件中的配置
@ConfigurationProperties注解的prefix属性,匹配application.yml全局配置文件中的一个属性值,然后映射到这个组件。

#注意,配置文件的赋值,是通过成员属性对应的setter方法。
@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person {

    private String lastName;
    private int age;
    private boolean boss;
    private Date birth;
    private Map maps;
    private List list;
    private Dog dog;
    

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", list=" + list +
                ", dog=" + dog +
                '}';
    }
}


四、@Value注解获取全局配置文件中内容

yml中配置内容

test:
  age: 2
  price: 3.14
  isOk: true
  birth: 2018/12/12
  date: 2019/11/11 21:59:43
  greet: 'hello \n world'
  greet1: 'hello\n world'

在java类中读取全局配置文件中的配置

@Component
@Data
public class Test {

    @Value("${test.age}")
    private int age;

    @Value("${test.price}")
    private float price;

    @Value("${test.birth}")
    private Date birth;

    @Value("${test.date}")
    private Date date;

    @Value("${test.greet}")
    private String greet;

    @Value("${test.greet1}")
    private String greet1;
    
}

五 @Value与@ConfigurationProperties对比

导入配置文件处理器,配置文件进行绑定时就会有提示。

<dependency>
	<groupId>org.springframe.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>

                                   @ConfigurationProperties @Value
功能 将配置中的属性批量注入到某个javaBean对象中 只能一次指定一个基本类型或string
松散绑定 支持短横线自动转驼峰 不支持
SPEL表达式 不支持 支持
JSR30数据绑定 支持数据验证注解 不支持
复杂数据类型 支持 不支持,只能用基本类型或string

如果说,我们只是在某个业务中需要获取匹配文件中的某项值,使用@Value;
如果说,我们专门编写一个javaBean来和配置文件进行映射,那么就使用@ConfigurationProperties( prefix = “xxx” )


总结:

  • @ConfigurationProperties( prefix=“xxx” ) 以及 @Value 用来获取全局配置文件application.yml中的内容。


六、使用 @PropertySource 读取其他的Properties配置文件
使用@Value进行赋值

#1.在source目录下创建dataSource.properties
username = root
password = 123123
dirverClassName = org.git.mm.mysql.Driver
url = jdbc:mysql://localhost;3306/test


#2.在DataSource.java中配置,使用@Value获取datasource.properties中的内容
@Data
@PropertySource(value ="classpath:datasource.properties")
@Component(value = "dataSource")
public class DataSource {

    @Value("${username}")
    private String username;
    @Value("${password}")
    private String password;
    @Value("${url}")
    private String url;
    @Value("${dirverClassName}")
    private String driverClassName;
}

使用@ConfigurationProperties进行赋值

#1.在source目录下创建dataSource.properties.
datasource.username = root
datasource.password = 123123
datasource.dirver-class-name = org.git.mm.mysql.Driver
datasource.url = jdbc:mysql://localhost;3306/test


#2.在DataSource.java中配置,使用@ConfigurationProperties(prefix="")获取datasource.properties中的内容.
@Data
@PropertySource(value ="classpath:datasource.properties")
@ConfigurationProperties(prefix = "datasource")
@Component(value = "dataSource")
public class DataSource {

    private String username;

    private String password;

    private String url;

    private String driverClassName;
}
发布了58 篇原创文章 · 获赞 34 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_36723759/article/details/104254275