4.加载指定配置文件

本文介绍了如何在SpringBoot中加载指定的配置文件,包括使用@PropertySource加载局部properties文件和通过@ImportResource引入XML配置文件。示例中详细展示了创建EmpService业务类,定义resources/spring01.xml文件,以及如何在主配置类中使用@ImportResource加载XML配置的过程。
摘要由CSDN通过智能技术生成

加载指定配置文件

1. @PropertySource 加载局部配置文件

@ConfigurationProperties 默认从全局配置文件(application.properties/application.yml)中获取值,
所有配置数据写在全局配置文件中,显得太臃肿了, 可将它们抽取出来,放到其他局部配置文件中。

@PropertySource :用于加载局部配置文件;

  1. 将 全局配置文件 中的emp相关配置数据 抽取 到 resources/ emp.properties 文件中
    emp.properties
emp.last-name=李四
emp.age=30
emp.birthday=1989/9/12
emp.boss=false
emp.salary=23000
emp.map.key1=value1
emp.map.key2=value2
emp.list=one, two, three
emp.forte.name=python
emp.forte.time=3
  1. @PropertySource :加载指定的配置文件; value 属性是数组类型, 用于指定文件位置
@PropertySource(value = {"classpath:emp.properties"})
@Component
@ConfigurationProperties(prefix = "emp")
@Validated
public class Emp {
private String lastName;
private Integer age;
private Doub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值