配置属性绑定详解
@configurationproperties
导入配置文件处理器,在配置文件进行数据绑定时就会有提示
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring‐boot‐configuration‐processor</artifactid>
<optional>true</optional>
</dependency>编写实体类,person 和 dog 的 javabean
package com.qcby.springboot.domain;
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(prefix = "person")
public class person {
private string lastname;
private integer age;
private boolean boss;
private date birth;
private map<string,string> maps;
private list<string> list;
private dog dog;
public person() {
}
public person(string lastname, integer age, boolean boss, date birth, map<string, string> maps, list<string> list, dog dog) {
this.lastname = lastname;
this.age = age;
this.boss = boss;
this.birth = birth;
this.maps = maps;
this.list = list;
this.dog = dog;
}
public string getlastname() {
return lastname;
}
public void setlastname(string lastname) {
this.lastname = lastname;
}
public integer getage() {
return age;
}
public void setage(integer age) {
this.age = age;
}
public boolean isboss() {
return boss;
}
public void setboss(boolean boss) {
this.boss = boss;
}
public date getbirth() {
return birth;
}
public void setbirth(date birth) {
this.birth = birth;
}
public map<string, string> getmaps() {
return maps;
}
public void setmaps(map<string, string> maps) {
this.maps = maps;
}
public list<string> getlist() {
return list;
}
public void setlist(list<string> list) {
this.list = list;
}
public dog getdog() {
return dog;
}
public void setdog(dog dog) {
this.dog = dog;
}
@override
public string tostring() {
return "person{" +
"lastname='" + lastname + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", list=" + list +
", dog=" + dog +
'}';
}
}@configurationproperties 注解用于告知 spring boot 将当前类的所有字段与配置文件中相关前缀对应的配置项进行批量绑定;
prefix = “person” 用于明确绑定配置文件中以 person 为前缀的所有子属性,与当前类字段进行一一映射;
关键前提是只有当当前组件是 spring 容器中的 bean(通过 @component 注解注册),才能使用容器提供的@configurationproperties属性绑定功能。
package com.qcby.springboot.domain;
public class dog {
private string name;
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 +
'}';
}
}application.yml 是 spring boot 全局固定名称的配置文件(与 application.properties 功能一致),以数据为核心,语法简洁、层级清晰,比 json/xml 更适合作为配置文件。
关键语法规则
缩进控制层级:必须用空格(不能用 tab),缩进量不固定,同一层级左对齐即可;
大小写敏感:属性名、值的大小写严格区分;
键值分隔:属性名和值之间用 : 分隔,且 : 后必须加空格;
yaml 核心值类型写法
- 字面量(数字、字符串、布尔)
字面量是最基础的配置值,直接对应 java 中的基本类型
数字 / 布尔:直接写值,无需引号;
字符串:默认无引号,特殊字符(如 \n)会被当作普通字符;
双引号(“”):不转义特殊字符,按原含义生效;
单引号(‘’):转义特殊字符,特殊字符仅作为普通字符串; - 对象 / map(键值对集合)
对应 java 中的 map 或自定义实体类;
嵌套式通过缩进表示层级,键值之间用 : 分隔(key: value);
行内式用 {key1: value1, key2: value2, …} 包裹,逗号分隔; - 数组 / list / set
元素支持字面量、对象等任意类型;
缩进式元素前加 - (横杠 + 空格)开头表示列表项,缩进一致;
行内式用 [值1, 值2, …] 包裹,逗号分隔; - 占位符使用(动态赋值)
引用已配置的属性:${属性名};
生成随机值:${random.value}、${random.int}、${random.int(10)}、${random.int[1024,65536]};
默认值:${属性名:默认值}(属性不存在时用默认值)。

将配置文件中 person 前缀下的所有属性,自动映射到 person 类的对应字段中,包括基本类型、日期、集合(list/map)、嵌套对象等,最终通过容器 bean 在项目中直接使用配置值。
需要注意的是测试单元的三级包与代码的三级包名称需要保持一致
package com.qcby.springboot;
import com.qcby.springboot.domain.person;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;
@runwith(springrunner.class)
@springboottest
public class springboottest {
@autowired
private person person;
@test
public void contextloads(){
system.out.println(person);
}
}运行 contextloads() 测试方法时,junit 会先读取 @runwith(springrunner.class),使用 spring 提供的 springrunner 运行器(其中 springrunner 是 springjunit4classrunner 的简化别名),springrunner 识别到 @springboottest 注解,触发 springboot 的自动配置,启动 spring boot 应用上下文(容器),容器启动过程中会进行扫描组件和属性绑定,扫描项目中的 @configuration 配置类,找到 person 类上的 @component 将其注册为容器 bean,通过 @configurationproperties(prefix = “person”) 读取配置文件中 person 前缀的属性,注入 person bean 的字段。容器启动完成后,@autowired person person 从容器中找到并注入测试类的 person bean,执行 contextloads() 方法,打印 person 对象,调用 tostring() 输出结果,测试结束后,spring 容器自动销毁,释放资源。

若测试单元的三级包与代码的三级包名称不一致,会出现 spring boot 测试启动时找不到核心配置类报错

spring 测试默认只扫描测试类所在的包(com.qcby.qcbyjy)及其子包,但主配置类在 com.qcby.springboot,不在扫描范围内,所以找不到。
验证yaml 字符串类型写法
双引号(“”)不转义特殊字符,按原含义生效,单引号(‘’)转义特殊字符,特殊字符仅作为普通字符串


java.util.date 传统日期类型默认支持斜杠分隔(yyyy/mm/dd)不默认支持横杠分隔(yyyy-mm-dd)格式

验证占位符的使用

@value 属性注入
@value 是一个核心注解,用于给 spring 管理的 bean 属性注入值
第一种常见注入场景:直接注入常量值
如果属性值是固定的常量,可以直接用 @value(“常量”) 注入属性值(字符串需加引号,数字 / 布尔无需,不支持复杂类型的注入),无需配置文件。
package com.qcby.springboot.domain;
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 person1 {
@value("zhangsan@123.com")
private string email;
@value("zhangsan")
private string lastname;
@value("#{11*2}")
private integer age;
@value("true")
private boolean boss;
@value("2025/11/12")
private date birth;
private map<string,object> maps;
private list<object> list;
private dog dog;
public string getemail() {
return email;
}
public void setemail(string email) {
email = email;
}
public string getlastname() {
return lastname;
}
public void setlastname(string lastname) {
this.lastname = lastname;
}
public integer getage() {
return age;
}
public void setage(integer age) {
this.age = age;
}
public boolean getboss() {
return boss;
}
public void setboss(boolean boss) {
this.boss = boss;
}
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 list;
}
public void setlists(list<object> lists) {
this.list = lists;
}
public dog getdog() {
return dog;
}
public void setdog(dog dog) {
this.dog = dog;
}
@override
public string tostring() {
return "person1{" +
"email='" + email + '\'' +
", lastname='" + lastname + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + list +
", dog=" + dog +
'}';
}
}
第二种注入配置文件(application.properties/yaml)中的值
package com.qcby.springboot.domain;
import org.springframework.beans.factory.annotation.autowired;
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 person2 {
@value("${person.email}")
private string email;
@value("${person.lastname}")
private string lastname;
@value("${person.age}")
private integer age;
@value("${person.boss}")
private boolean boss;
@value("${person.birth}")
private date birth;
private map<string,object> maps;
private list<object> list;
@autowired
private dog1 dog1;
public string getemail() {
return email;
}
public void setemail(string email) {
email = email;
}
public string getlastname() {
return lastname;
}
public void setlastname(string lastname) {
this.lastname = lastname;
}
public integer getage() {
return age;
}
public void setage(integer age) {
this.age = age;
}
public boolean getboss() {
return boss;
}
public void setboss(boolean boss) {
this.boss = boss;
}
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> getlist() {
return list;
}
public void setlist(list<object> list) {
this.list = list;
}
public dog1 getdog1() {
return dog1;
}
public void setdog1(dog1 dog1) {
this.dog1 = dog1;
}
@override
public string tostring() {
return "person2{" +
"email='" + email + '\'' +
", lastname='" + lastname + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", list=" + list +
", dog1=" + dog1 +
'}';
}
}package com.qcby.springboot.domain;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;
@component
public class dog1 {
@value("${person.dog.name}")
private string name;
@value("${person.dog.age}")
private integer age;
public dog1() {
}
public dog1(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 "dog1{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

@validated 数据校验
spring boot 2.3.0 之前,spring-boot-starter-web 会默认传递依赖 spring-boot-starter-validation,而该 starter 已包含 hibernate validator(数据校验实现核心)。
spring boot 2.3.0 发布后,官方将 spring-boot-starter-validation 从 spring-boot-starter-web/spring-boot-starter-webflux 的默认传递依赖中移除,因此,若需使用 @validated 实现参数校验,必须手动引入 spring-boot-starter-validation,否则仅能使用注解标记,校验逻辑不会执行(无报错,仅不生效)。
<!-- 2.3.0 之后的版本需手动引入校验 starter -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-validation</artifactid>
</dependency>package com.qcby.springboot.domain;
import org.springframework.beans.factory.annotation.value;
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.stereotype.component;
import org.springframework.validation.annotation.validated;
import javax.validation.constraints.notnull;
import java.util.date;
import java.util.list;
import java.util.map;
@component
@configurationproperties(prefix = "person")
@validated
public class person3 {
@javax.validation.constraints.email
@value("${person.email}")
private string email;
@notnull
@value("${person.lastname}")
private string lastname;
private integer age;
private boolean boss;
private date birth;
private map<string,object> maps;
private list<object> list;
private dog dog;
public person3() {
}
public person3(@javax.validation.constraints.email string email, integer age, boolean boss, date birth, map<string, object> maps, list<object> list, dog dog) {
email = email;
this.age = age;
this.boss = boss;
this.birth = birth;
this.maps = maps;
this.list = list;
this.dog = dog;
}
public string getemail() {
return email;
}
public void setemail(string email) {
email = email;
}
public string getlastname() {
return lastname;
}
public void setlastname(string lastname) {
this.lastname = lastname;
}
public integer getage() {
return age;
}
public void setage(integer age) {
this.age = age;
}
public boolean getboss() {
return boss;
}
public void setboss(boolean boss) {
this.boss = boss;
}
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> getlist() {
return list;
}
public void setlist(list<object> list) {
this.list = list;
}
public dog getdog() {
return dog;
}
public void setdog(dog dog) {
this.dog = dog;
}
@override
public string tostring() {
return "person3{" +
"email='" + email + '\'' +
", lastname='" + lastname + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", list=" + list +
", dog=" + dog +
'}';
}
}package com.qcby.springboot.domain;
public class dog {
private string name;
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 +
'}';
}
}
email 被 @email 注解修饰,配置文件输入的属性 email 邮箱不合法时会报错

lastname 被 @notnull 注解修饰,配置文件输入的属性 lastname 为空时会报错

@javax.validation.constraints.email
@value(“zhangsan”)
连个注解同时使用的时候邮箱校验就不生效了

@value获取值和@configurationproperties获取值比较

验证松散绑定的区别
package com.qcby.springboot.domain;
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(prefix = "person")
//@validated
public class person4 {
private string email;
//@value("${person.last_name}")
private string last_name;
private integer age;
private boolean boss;
private date birth;
private map<string,object> maps;
private list<object> list;
private dog dog;
public person4() {
}
public person4(string email, integer age, boolean boss, date birth, map<string, object> maps, list<object> list, dog dog) {
email = email;
this.age = age;
this.boss = boss;
this.birth = birth;
this.maps = maps;
this.list = list;
this.dog = dog;
}
public string getemail() {
return email;
}
public void setemail(string email) {
email = email;
}
public string getlast_name() {
return last_name;
}
public void setlast_name(string last_name) {
this.last_name = last_name;
}
public integer getage() {
return age;
}
public void setage(integer age) {
this.age = age;
}
public boolean getboss() {
return boss;
}
public void setboss(boolean boss) {
this.boss = boss;
}
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> getlist() {
return list;
}
public void setlist(list<object> list) {
this.list = list;
}
public dog getdog() {
return dog;
}
public void setdog(dog dog) {
this.dog = dog;
}
@override
public string tostring() {
return "person4{" +
"email='" + email + '\'' +
", last_name='" + last_name + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", list=" + list +
", dog=" + dog +
'}';
}
}
@configurationproperties 支持配置名与 java 属性名的格式自动转换

@value 不支持配置名与 java 属性名的格式自动转换,必须严格匹配配置名(大小写、分隔符完全一致)

加载指定的配置文件
@propertysource 加载自定义配置文件
加载非默认的配置文件 person.properties,补充 spring 环境中的配置属性,加载后的配置属性可通过 @value 或 @configurationproperties 直接获取。
@importresource 导入传统的 spring xml 配置文件 spring-bean.xml,让 xml 中定义的 bean 被 spring 容器管理。spring boot 默认不扫描 xml 配置文件,这个注解是为了兼容旧项目。
示例代码:

package com.qcby.springboot;
import com.qcby.springboot.service.helloservice;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.importresource;
import org.springframework.context.annotation.propertysource;
@configuration
@propertysource("classpath:config.properties")
@importresource(locations = "classpath:spring-bean.xml")
public class myappconfig {
}package com.qcby.springboot.service;
import org.springframework.beans.factory.annotation.value;
public class helloservice {
@value("${hello.service.version}")
private string version;
public void sayhello() {
system.out.println("helloservice 业务版本:" + version);
}
}<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloservice" class="com.qcby.springboot.service.helloservice"></bean>
</beans>package com.qcby.springboot;
import com.qcby.springboot.service.helloservice;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.annotation.qualifier;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;
@runwith(springrunner.class)
@springboottest
public class springboottest {
@autowired
private helloservice helloservice;
@test
public void testpropertyinjection() {
helloservice.sayhello();
}
}
整体核心逻辑:通过 @springboottest 启动 spring 容器,容器自动扫描并加载 @configuration 配置类,先通过 @propertysource 读取自定义配置文件,再通过 @bean 注册 helloservice 实例到容器,最后 @autowired 从容器中取出该实例,调用方法验证配置注入是否成功。
@bean 时 javaconfig 方式注册 bean
在 @configuration 标注的配置类中,通过方法手动注册 bean 到 spring 容器,spring 会管理 bean 的生命周期(创建、依赖注入、销毁)。
package com.qcby.springboot;
import com.qcby.springboot.service.helloservice;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.propertysource;
@configuration
@propertysource("classpath:config.properties")
public class myappconfig {
@bean
public helloservice helloservice01() {
system.out.println("配置类@bean注册组件:helloservice01");
return new helloservice();
}
}
整体核心逻辑:测试类通过 @springboottest 启动 spring 容器,容器加载 @configuration 配置类后,通过 @propertysource 读取自定义配置文件、@importresource 导入 xml 配置并注册 helloservice 类型 bean,spring 自动为该 bean 注入配置文件中的版本属性,最终测试类通过 @autowired 注入 bean 并调用方法验证属性注入效果。
到此这篇关于springboot 配置属性绑定详解的文章就介绍到这了,更多相关springboot 配置属性内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论