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

相关推荐
程序员大金31 分钟前
基于SpringBoot+Vue+MySQL的垃圾分类回收管理系统
java·vue.js·spring boot·后端·mysql·mybatis
coding侠客38 分钟前
Spring Boot 注解探秘:常用配置值读取注解的魔力
java·spring boot·后端·spring·spring cloud
程序员大金1 小时前
基于SpringBoot+Vue+MySQL的影院购票系统
java·vue.js·spring boot·后端·mysql·mybatis
工业甲酰苯胺4 小时前
Springboot中自定义监听器
java·spring boot·spring
一粟1025 小时前
Spring Boot:现代化Java应用开发的艺术
java·spring boot·后端
IT研究室5 小时前
计算机毕业设计选题推荐-校园车辆管理系统-Java/Python项目实战(亮点:数据可视化分析、账号锁定)
java·spring boot·python·django·毕业设计·源码·课程设计
coffee_baby5 小时前
桥接模式详解和分析JDBC中的应用
java·数据库·spring boot·java-ee·桥接模式
SuperherRo6 小时前
spring综合性利用工具-SpringBootVul-GUI(五)
spring boot·spring·漏洞利用·信息泄露
customer087 小时前
【开源免费】基于SpringBoot+Vue.JS在线视频教育平台(JAVA毕业设计)
java·jvm·vue.js·spring boot·spring cloud·java-ee·intellij-idea
DEARM LINER9 小时前
dubbo 服务消费原理分析之应用级服务发现
spring boot·架构·服务发现·dubbo·safari