springboot 读取配置文件错误

今天再写springboot读取自定义配置文件时,启动项目报错:

***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
Configuration property name 'WebConfig' is not valid:
 
    Invalid characters: 'C'
    Bean: WebConfig
    Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter

该问题出现在springboot 2.0之后的版本,主要是说配置文件不能使用驼峰和分割应该用'-’ .

 

我的配置文件为WebConfig.properties

webConfig.map.test1=111
webConfig.map.test2=1111
webConfig.map.test3=11111

在对应的实体类webConfig.java

@Configuration
@PropertySource("classpath:webConfig .properties")
@ConfigurationProperties(prefix = "webConfig")
@Data
public class webConfig implements Serializable {


    private static final long serialVersionUID = -4484635222481681542L;
    private Map<String,String> map=new HashMap<>();


}

启动后会报错。

解决方案

 

将配置文件的驼峰修改为小写(将webConfig改为webconfig,全部小写):

webconfig.map.test1=111
webconfig.map.test2=1111
webconfig.map.test3=11111

实体类修改为小写(prefix = "webConfig" 改为 prefix = "webconfig",改成小写):

@Configuration
@PropertySource("classpath:webConfig .properties")
@ConfigurationProperties(prefix = "webconfig")
@Data
public class webConfig implements Serializable {


    private static final long serialVersionUID = -4484635222481681542L;
    private Map<String,String> map=new HashMap<>();


}

然后启动正常,不报错。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值