SpringBoot标签之@ConfigurationProperties、@PropertySource注解的使用
当获取主配置文件中属性值时,只需@ConfigurationProperties(prefix = "person")注解来修饰某类,其作用是告诉springBoot,此类中的属性将与默认的全局配置文件中对应属性一一绑定。属性名必须是application.yml或application.properties。【prefix = "person"】表示与配置文件中哪个层级的属性进行绑定。
当一些属性不想配置到主配置文件,需自定义一个配置文件,需通过@PropertySource注解指定此配置文件路径。
而@ConfigurationProperties(prefix = "xxx")注解指定自定义配置文件中哪个层级属性需绑定。
配置文件的位置:\src\main\resources\application.yml
一、bean类与主配置文件的绑定
案例:bean类Person.java
@Component
@ConfigurationProperties(prefix = "person")//只能从默认的全局文件中获取
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
//======================get 、set方法省略=============
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
配置文件:
person:
lastName: fang \n xin \n de
age: 18
boss: false
birth: 2018/12/10
maps: {a1: fang, a2: li,a3: zhang}
lists: [cat,pig,dog]
dog:
name: xiaogou10号
age: 1
测试案例:
package com.atguigu.springboot;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
打印结果:
Person{lastName='fang \n xin \n de', age=18, boss=false, birth=Mon Dec 10 00:00:00 CST 2018, maps={a1=fang, a2=li, a3=zhang}, lists=[cat, pig, dog], dog=Dog{name='xiaogou10号', age=1}}
二、bean类与自定义的配置文件绑定,需@ConfigurationProperties、@PropertySource两个注解一起修饰bean类
@PropertySource指定自定义配置文件的地址
@ConfigurationProperties指定绑定属性的层级
@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value ={"classpath:person.properties"})
@Validated
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
//==================set、get方法省略=======================
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
自定义配置文件person.properties:
person.lastName=李次方1110
person.age=12
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog01
person.dog.age=15
打印的结果:
Person{lastName='李次方1110', age=12, boss=false, birth=Fri Dec 15 00:00:00 CST 2017, maps={k1=v1, k2=14}, lists=[a, b, c], dog=Dog{name='dog01', age=15}}
三、@ConfigurationProperties注解的特点
1.批量注入配置文件中值
2.支持松散绑定
配置文件中属性格式分别为lastname 、last_name、last-name 时,都可以与Person类中属性lastName绑定
person:
lastname: fang \n xin \n de
3.支持JSR303校验:
Person类上加上@Validated,表示类中属性需要进行验证。 @Email表示lastName必须满足邮件格式
@Component
@ConfigurationProperties(prefix = "person")//只能从默认的全局文件中获取
@Validated
public class Person {
@Email
private String lastName;
}
配置文件中lastname值不是邮件格式
person:
lastname: fang \n xin \n de
输出person对应的实例时报错:
4.能够封装复杂结构的属性:例如:map集合、级联属性
5.不支持SpEL表达式
参考:SpringBoot之@ConfigurationProperties、@PropertySource注解的使用
本文来自博客园,作者:aspirant,转载请注明原文链接:https://www.cnblogs.com/aspirant/p/10298611.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2018-01-21 Android studio出现Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"的解决办法