likes
comments
collection
share

Spring Boot Properties Prefix Must Be in Canonical Form

作者站长头像
站长
· 阅读数 4

概述

本文对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 parameter使用的命名约定应遵循kebab case大小写规范。

例如:

@ConfigurationProperties(prefix = "my-example")

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

Maven依赖

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

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter</artifactId>      
    <version>3.0.5</version> 
</dependency>

重现错误

Application Configuration

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

下面在springboot中注册所需组件

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

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

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

|   pom.xml
+---src
|   +---main
|   |   +---java
|   |   |   \---com
|   |   |       \---baeldung
|   |   |           ...
|   |   |           ...
|   |   \---resources
|   |           application.properties

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

$ 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配置文件中,我们可以使用任何变量命名风格(Camel Case、Pascal Case、Snake Case、Upper Case等)。

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

本文为本人翻译,原文出处为Baeldung网站 原文链接,版权归Baeldung网站及原作者所有。