SpringBoot读取properties(yml)文件中配置信息
Springboot读取YML配置文件
@Value
该注解作用的作用是将我们配置文件的属性读出来,有@Value(“${}”)和@Value(“#{}”)两种方式
① ${ property : default_value }
② #{ obj.property? :default_value }
application-prod.yml配置文件内容如下:
server:
port: 80
rrc:
name: lq
birthday: 199009
admin: menggudashuishen
对应的JAVA代码
@Value("${admin}")
private String admin;
@Value("${server.port}")
private String port;
#{}是不能以第一种方式来进行取值,否则会报错。#{}里面包含的是obj,所以需要配合bean来使用,现在创建一个UserBean,并且给某个字段加上@Value(“#{}”)注解(按自己的需求任意添加注解)
定义Bean:
package com.rrc.dto.base;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author: wangql
* @Title: Rrc
* @ProjectName: SpringBoot
* @Description:
* @date: 2022/4/27 0:40
*/
@Component
@ConfigurationProperties("rrc")
public class Rrc {
private String name;
private String birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
获取值:
@Value("#{rrc.birthday}")
private String birthday;
SpringBoot中使用@Value()只能给普通变量注入值,不能直接给静态变量赋值。如果是静态变量那么拿到的属性值则为null。解决办法是给其属性值加上set方法,并且给其类上添加@Component注解
@Value
这个注解估计很熟悉了,Spring中从属性取值的注解,支持SPEL
表达式,不支持复杂的数据类型,比如List
。
@ConfigurationProperties
使用@Value
注解可以直接读取application.yml配置文件中的配置信息。
使用@ConfigurationProperties
也可以读取application.yml文件中的配置信息。
使用@ConfigurationProperties
和@PropertySource
可以读取指定properties配置文件中的配置信息。
@ConfigurationProperties注解提供了我们将多个配置选项注入复杂对象的能力。它要求我们指定配置的共同前缀。支持从驼峰camel-case到短横分隔命名kebab-case的自动转换。
ini.properties
rrc.name-=wql11
rrc.birthday=19990510
对应的实体类为
package com.rrc.dto.base;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @author: wangql
* @Title: Rrc
* @ProjectName: SpringBoot
* @Description:
* @date: 2022/4/27 0:40
*/
@Component
@PropertySource("classpath:ini.properties")
@ConfigurationProperties("rrc")
public class Rrc {
private String name;
private String birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
@ConfigurationProperties
注解能够很轻松的从配置文件中取值,优点如下:
- 支持批量的注入属性,只需要指定一个前缀
prefix
- 支持复杂的数据类型,比如
List
、Map
- 对属性名匹配的要求较低,比如
user-name
,user_name
,userName
,USER_NAME
都可以取值 - 支持JAVA的JSR303数据校验
注意:@ConfigurationProperties
这个注解仅仅是支持从Spring Boot的默认配置文件中取值,比如application.properties
、application.yml
多环境配置
自定义配置文件是我们日常开发中经常会使用的资源,而spring只提供了类似application-* 的这中匹配方式,并不支持我们自定义的配置文件名称,例如:customize-dev.properties,但是spring提供了一个注解可以方便加载我们的自定义配置文件,它就是@PropertySource
自定义配置文件
ini-dev.properties
rrc.name-=wql22
rrc.birthday=19990510
对应的实体类
package com.rrc.dto.base;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @author: wangql
* @Title: Rrc
* @ProjectName: SpringBoot
* @Description:
* @date: 2022/4/27 0:40
*/
@Component
@PropertySource("classpath:ini-${customize.profiles.active}.properties")
@ConfigurationProperties(prefix = "rrc", ignoreInvalidFields = true)
public class Rrc {
private String name;
private String birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
启动参数:
-Dcustomize.profiles.active=dev
自定义配置文件获取值
Spring Boot在启动的时候会自动加载application.xxx
和bootsrap.xxx
,但是为了区分,有时候需要自定义一个配置文件,那么如何从自定义的配置文件中取值呢?此时就需要配合@PropertySource
这个注解使用了。
只需要在配置类上标注@PropertySource
并指定你自定义的配置文件即可完成。如下