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

相关推荐
Rover.x29 分钟前
Netty基于SpringBoot实现WebSocket
spring boot·后端·websocket
中国胖子风清扬1 小时前
SpringAI和 Langchain4j等 AI 框架之间的差异和开发经验
java·数据库·人工智能·spring boot·spring cloud·ai·langchain
Java水解2 小时前
【SpringBoot3】Spring Boot 3.0 集成 Mybatis Plus
spring boot·后端
哈哈老师啊2 小时前
Springboot校园订餐管理系统k2pr7(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
JIngJaneIL2 小时前
基于java+ vue学生选课系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
残花月伴3 小时前
天机学堂-day4(高并发优化方案)
java·spring boot·后端
阿拉斯攀登3 小时前
Spring Boot ——入门与实战
spring boot·springboot
JIngJaneIL3 小时前
基于java+ vue建筑材料管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
一 乐3 小时前
办公系统|基于springboot + vueOA办公管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·spring
利刃大大4 小时前
【SpringBoot】SpringMVC && 请求注解详解 && 响应注解详解 && Lombok
java·spring boot·后端