springboot 读取配置文件_SpringBoot读取自定义配置文件的解决方案,请查收!

c25ea57e27c6fe4567d85b19a59f4a3d.png

本篇文章的主题是:在SpringBoot项目中,如何读取自定义配置文件?


基于SpringBoot版本,读取自定义配置文件有以下2种方案:

  • 基于SpringBoot 2.x,使用@PropertySource和@ConfigurationProperties共同标注一个JavaBean
  • 基于SpringBoot 2.0以下,使用@ConfigurationProperties标注JavaBean
e984aa6139df94682cab8d0ef4398475.png

具体,参见下方示例:


在类路径下的config文件夹下新建jdbc.properties文件,配置项如下:

jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true jdbc.userName=root jdbc.password=root

接着,创建JavaBean作为以上配置项的数据载体

  • 基于SpringBoot 2.x
@Configuration @ConfigurationProperties(prefix = "jdbc") @PropertySource("classpath:config/jdbc.properties")public class JdbcConfig {  private String driverClassName; private String url;  private String userName;  private String password; // 省略Getter、Setter } 

  • 基于SpringBoot 2.0以下版本
@Configuration @ConfigurationProperties(prefix = "jdbc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值