You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2中@Bean定义的Bean名称是否必须采用kebab case格式?

Spring Boot 2 @Bean Naming: No Mandatory Kebab Case Requirement

Great question—this is a super common point of confusion because Spring Boot 2's stricter configuration binding rules get mixed up with bean naming conventions. Let’s break this down clearly:

1. First, the configuration binding context (why you heard about kebab case)

Spring Boot 2 does enforce kebab case for configuration property prefixes when binding external properties (like from application.properties or environment variables) to your classes. For example, if you have a @ConfigurationProperties(prefix = "my-app.settings") annotation, the prefix must use kebab case—camelCase or snake_case here would cause binding failures. This was a deliberate change to standardize external configuration naming across different sources.

2. @Bean naming: No kebab case requirement

When defining beans with the @Bean annotation, there is no mandatory requirement to use kebab case. Here’s the breakdown:

  • By default, a bean’s name is the name of the method that defines it. Java method names don’t even support kebab case (since hyphens aren’t allowed in identifiers), so this isn’t feasible unless you explicitly set the name via the @Bean annotation.
  • You can name your beans using camelCase, snake_case (if you use underscores in method names, though that’s not standard Java), or even kebab case by specifying the name explicitly:
    // CamelCase method name (default bean name: userService)
    @Bean
    public UserService userService() {
        return new UserService();
    }
    
    // Explicit kebab case bean name (totally optional)
    @Bean("user-service")
    public UserService anotherUserService() {
        return new UserService();
    }
    
  • Spring Boot 2 didn’t alter the core Spring Framework’s bean naming rules—this flexibility remains entirely up to your project’s conventions.

3. Why the confusion?

The mix-up likely comes from conflating configuration property binding rules with bean naming. If you’re using configuration properties to conditionally create beans (e.g., @ConditionalOnProperty), those properties will need kebab case, but the bean itself can still use any valid name you choose.

As you noticed, the Spring Boot 2 migration docs don’t mention this because it’s not a breaking change or mandatory rule—bean naming flexibility was preserved.

内容的提问来源于stack exchange,提问作者blackcompe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:43:18