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来访问它们,从而降低了由不一致的命名约定引起的错误或混淆的可能性。

相关推荐
GEMjay1 小时前
对于SpringBoot的三层缓存的思考
spring boot
泉城老铁2 小时前
导出大量数据时如何优化内存使用?SXSSFWorkbook的具体实现方法是什么?
spring boot·后端·excel
泉城老铁2 小时前
springboot实现对接poi 导出excel折线图
java·spring boot·后端
稚辉君.MCA_P8_Java3 小时前
kafka解决了什么问题?mmap 和sendfile
java·spring boot·分布式·kafka·kubernetes
Lisonseekpan5 小时前
Spring Boot 中使用 Caffeine 缓存详解与案例
java·spring boot·后端·spring·缓存
Terio_my6 小时前
Spring Boot Web环境测试配置
spring boot
汤姆yu6 小时前
2025版基于springboot的美食食品商城系统
spring boot·后端·美食
kfepiza7 小时前
Spring 如何解决循环依赖 笔记251008
java·spring boot·spring
Arva .8 小时前
Spring Boot 配置文件
java·spring boot·后端
IT_Octopus8 小时前
https私人证书 PKIX path building failed 报错解决
java·spring boot·网络协议·https