今天再写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<>();
}
然后启动正常,不报错。