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

使用Spring @ConfigurationProperties注解时,非Getter方法命名不能以“get”开头是否有官方文档说明?

Why does a method starting with "get" cause ConfigurationPropertiesBindException in Spring Boot?

Great question! Let's break this down step by step, including references to Spring's official documentation and the underlying rules at play here.

First, let's recap your scenario: you have a Spring Boot class combining @Component, @ConfigurationProperties, and @Scheduled, with a method named getSomething() that triggers a ConfigurationPropertiesBindException on startup. Renaming the method (e.g., to putSomething()) resolves the issue, and you're curious about the official naming rules that explain this behavior.

Here's your original code for context:

@Component
@Data
@EnableScheduling
@ConfigurationProperties(prefix = "demo")
public class DemoClass {
    private String aString;
    private Long aLong;
    
    @Scheduled(fixedDelayString = "${demo.delay}")
    void getSomething() {
        System.out.println("aString = " + aString);
        System.out.println("aLong = " + aLong.toString());
    }
}

Spring's JavaBean Naming Rules & Property Binding

Spring's @ConfigurationProperties binding mechanism strictly adheres to the JavaBean Specification when inferring properties from class methods. Here's what the official documentation states:

  1. Property Accessor Naming (Spring Core Documentation)

    For a property named foo, the standard getter method is getFoo() (or isFoo() for boolean properties), and the corresponding setter is setFoo(). Any method starting with get that follows this camel-case pattern is treated as a getter for a matching property—regardless of your intended purpose for the method.

  2. @ConfigurationProperties Behavior (Spring Boot Documentation)
    When using @ConfigurationProperties, Spring scans the target class for methods that match JavaBean getter/setter patterns to map external properties (from application.properties, environment variables, etc.) to class members. If it detects a getXxx() method, it expects a corresponding xxx property to exist (either as a class field or a setter method to populate it).

Why Your Code Threw the Exception

In your case, getSomething() fits the getter pattern for a property named something. Spring's property binder attempts to find a demo.something property (matching your demo prefix) to bind to this inferred "virtual" property. Since there's no corresponding something field or setter in DemoClass, this mismatch triggers the ConfigurationPropertiesBindException.

Crucially, Spring's binding logic doesn't differentiate between method purposes—it only evaluates the naming pattern. Even though your method is a scheduled task, not a true property getter, the get prefix triggers the property inference logic.

Key Takeaway

While using get as a prefix for non-getter methods is an unorthodox coding practice, Spring's behavior here is explicitly aligned with the JavaBean specification, which is clearly documented in both the Spring Core and Spring Boot official docs. To avoid this issue, ensure any non-accessor methods don't follow the getXxx()/setXxx() naming convention.

内容的提问来源于stack exchange,提问作者Ivan A. Malich

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 13:27:33