I have a pretty vanilla Spring Boot v2.6.4 Java application, using Java 17.
I am trying to get records to work for my @ConfigurationProperties, but having little success.
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "custom")
@ConstructorBinding
public record CustomConfigProperties(String path) { }
This fails with:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: @Configuration class 'CustomConfigProperties' may not be final. Remove the final modifier to continue.
Obviously, records are implicitly final, so I am stumped. No amount of googling yields anything useful. The closest I could find was this SO question, but it's not really directly related Infact a commenter on there has the same issue as me)
CodePudding user response:
@Configuration
should be used on a class that defines beans via @Bean
methods. Annotating with @Configuration
triggers the creation of a CGLib proxy to intercept calls between these @Bean
methods. It is this proxying attempt that is failing because the record is implicitly final.
You should remove @Configuration
from your record and then enable your @ConfigurationProperties
record using @ConfigurationPropertyScan
or @EnableConfigurationProperties(CustomConfigProperties.class)
. You can also remove @ConstructorBinding
as records are implicitly constructor bound. @ConstructorBinding
need only be used on a record if the record has multiple constructors and you need to indicate which constructor should be used for property binding.
CodePudding user response:
It works if I explicitly annotate my main Application class with:
@ConfigurationPropertiesScan
Which is ridiculous since a plain old class style works without this annotation.