使用Spring @ConfigurationProperties注解时,非Getter方法命名不能以“get”开头是否有官方文档说明?
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:
Property Accessor Naming (Spring Core Documentation)
For a property named
foo, the standard getter method isgetFoo()(orisFoo()for boolean properties), and the corresponding setter issetFoo(). Any method starting withgetthat follows this camel-case pattern is treated as a getter for a matching property—regardless of your intended purpose for the method.@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 (fromapplication.properties, environment variables, etc.) to class members. If it detects agetXxx()method, it expects a correspondingxxxproperty 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

