Spring Boot Properties Prefix Must Be in Canonical Form

概述

本文对SpringBoot项目启动过程中出现的 "Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters, and must start with a letter" 错误进行详细分析。

首先,我们将阐明Spring Boot中出现此错误的主要原因,之后通过一个实际的例子深入探讨如何重现和解决该问题。

问题描述

首先,让我们了解错误消息的含义,"Canonical names should be kebab-case",("kebab-case"已成为一种流行的命名约定。在编程中,kebab-case指的是一种命名风格,其中单词通过短横线 "-" 连接,所有字母均为小写。这种命名风格通常用于变量名、函数名、文件名等标识符的命名中,以提高代码的可读性和一致性)。该错误信息告诉我们规范名称(规范名称是指可唯一标识一个属性的属性名称)应该是kebab case。

为了确保一致性,@ConfigurationProperties注解中的prefix参数使用的命名约定应遵循kebab大小写。

例如:

java 复制代码
@ConfigurationProperties(prefix = "my-example")

在上述代码片段中,前缀my-example应该遵循kebab的大小写惯例。

Maven依赖

由于这是一个基于maven的项目,需要将必要的依赖项添加到pom.xml中

xml 复制代码
<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter</artifactId>      
    <version>3.0.5</version> 
</dependency>

要重现这个问题,spring-boot-starter是唯一需要的依赖项。

重现错误

Application Configuration

如果不熟悉springboot的Configuration properties,可以通过 Guide to ConfigurationProperties in Spring Boot文章进行学习。

下面在springboot中注册所需组件

java 复制代码
@Configuration 
@ConfigurationProperties(prefix = "customProperties") 
public class MainConfiguration { 
    String name; 
    // getters and setters
}

然后,我们需要向application.properties文件添加一个自定义属性 custom-properties.name="Baeldung"

application.properties位于src/main/resources下:

css 复制代码
|   pom.xml
+---src
|   +---main
|   |   +---java
|   |   |   \---com
|   |   |       \---baeldung
|   |   |           ...
|   |   |           ...
|   |   \---resources
|   |           application.properties

现在运行springboot的示例程序,报错信息如下:

markdown 复制代码
$ mvn spring-boot:run
...
...
***************************
APPLICATION FAILED TO START
***************************

Description:

Configuration property name 'customProperties' is not valid:

    Invalid characters: 'P'
    Bean: mainConfiguration
    Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter

Action:

Modify 'customProperties' so that it conforms to the canonical names requirements.

如上所示,我们得到一条错误消息 Modify 'customProperties' so that it conforms to the canonical names requirements。此错误消息表示当前用于customProperties的命名约定未遵循Spring设置的命名约定,换换言之,需要更改customProperties的名称以符合Spring中命名属性的要求。

##修复错误 我们需要将属性前缀 @ConfigurationProperties(prefix = "customProperties") 修改为kebab大小写风格: @ConfigurationProperties(prefix = "custom-properties")

而在properties配置文件中,我们可以保留任何样式。

Kebab风格的优势

在访问properties配置文件时候,使用kebab case的主要优势是,配置文件中的属性名称可以使用以下任何一种大小写风格:

  • camelCaseLikeThis
  • PascalCaseLikeThis
  • snake_case_like_this
  • kebab-case-like-this 并使用kebab casing来访问这些属性

示例: @ConfigurationProperties(prefix = "custom-properties") 可以访问到以下的properties customProperties.name="Baeldung" CustomProperties.name="Baeldung" custom_properties.name="Baeldung" custom-properties.name="Baeldung"

结论

在本文中,我们了解到Spring Boot支持多种格式,包括property名字中的camel case、snake case和kebab case,但鼓励我们使用kebab case来访问它们,从而降低了由不一致的命名约定引起的错误或混淆的可能性。

相关推荐
天河归来18 分钟前
springboot框架redis开启管道批量写入数据
java·spring boot·redis
合作小小程序员小小店23 分钟前
web网页,在线%食谱推荐系统%分析系统demo,基于vscode,uniapp,vue,java,jdk,springboot,mysql数据库
vue.js·spring boot·vscode·spring·uni-app
sniper_fandc1 小时前
SpringBoot系列—MyBatis(xml使用)
java·spring boot·mybatis
咖啡啡不加糖2 小时前
RabbitMQ 消息队列:从入门到Spring Boot实战
java·spring boot·rabbitmq
代码老y5 小时前
Spring Boot项目中大文件上传的高级实践与性能优化
spring boot·后端·性能优化
Xiao_zuo_ya5 小时前
SpringBoot-Freemarker导出word
spring boot·word
kangkang-7 小时前
PC端基于SpringBoot架构控制无人机(二):MavLink协议
java·spring boot·后端·无人机
麦兜*8 小时前
Spring Boot秒级冷启动方案:阿里云FC落地实战(含成本对比)
java·spring boot·后端·spring·spring cloud·系统架构·maven
魂兮归乡9 小时前
B2、进度汇报(— 25/06/16)
经验分享·spring boot·ai·vue·团队开发·课程设计·web app
Q_Q5110082859 小时前
python的保险业务管理与数据分析系统
开发语言·spring boot·python·django·flask·node.js·php