ConfigurationProperties constructor @Autowired detection fails for proxy classes
See original GitHub issueSpring Boot 3. See minimized example project: https://github.com/matthenry87/configprops-bug
@Configuration
@ConfigurationProperties(prefix = "foo")
static class ConfigPropertiesConfigBean {
private final Environment environment;
private String bar;
public ConfigPropertiesConfigBean(Environment environment) {
this.environment = environment;
}
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configpropsBugApplication.ConfigPropertiesConfigBean' defined in file [C:\workspace\configprops-bug\target\classes\org\matthenry87\configpropsbug\ConfigpropsBugApplication$ConfigPropertiesConfigBean.class]: Cannot bind @ConfigurationProperties for bean 'configpropsBugApplication.ConfigPropertiesConfigBean'. Ensure that @ConstructorBinding has not been applied to regular bean
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:606) ~[spring-beans-6.0.0-RC2.jar:6.0.0-RC2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~[spring-beans-6.0.0-RC2.jar:6.0.0-RC2]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.0-RC2.jar:6.0.0-RC2]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.0-RC2.jar:6.0.0-RC2]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.0-RC2.jar:6.0.0-RC2]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.0-RC2.jar:6.0.0-RC2]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:931) ~[spring-beans-6.0.0-RC2.jar:6.0.0-RC2]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:916) ~[spring-context-6.0.0-RC2.jar:6.0.0-RC2]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[spring-context-6.0.0-RC2.jar:6.0.0-RC2]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-3.0.0-RC1.jar:3.0.0-RC1]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) ~[spring-boot-3.0.0-RC1.jar:3.0.0-RC1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-3.0.0-RC1.jar:3.0.0-RC1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[spring-boot-3.0.0-RC1.jar:3.0.0-RC1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[spring-boot-3.0.0-RC1.jar:3.0.0-RC1]
at org.matthenry87.configpropsbug.ConfigpropsBugApplication.main(ConfigpropsBugApplication.java:14) ~[classes/:na]
Caused by: java.lang.IllegalStateException: Cannot bind @ConfigurationProperties for bean 'configpropsBugApplication.ConfigPropertiesConfigBean'. Ensure that @ConstructorBinding has not been applied to regular bean
at org.springframework.util.Assert.state(Assert.java:76) ~[spring-core-6.0.0-RC2.jar:6.0.0-RC2]
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.bind(ConfigurationPropertiesBindingPostProcessor.java:86) ~[spring-boot-3.0.0-RC1.jar:3.0.0-RC1]
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:78) ~[spring-boot-3.0.0-RC1.jar:3.0.0-RC1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:420) ~[spring-beans-6.0.0-RC2.jar:6.0.0-RC2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745) ~[spring-beans-6.0.0-RC2.jar:6.0.0-RC2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599) ~[spring-beans-6.0.0-RC2.jar:6.0.0-RC2]
... 14 common frames omitted
Issue Analytics
- State:
- Created 10 months ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
ConfigurationProperties no working when @Autowired on ...
I have to do @Autowired on the constructor of the Bean to avoid legacy system to break. Below is the example @Component public...
Read more >Core Features - Spring
To opt out of constructor binding for a class with a single parameterized constructor, the constructor must be annotated with @Autowired .
Read more >Why are my autowired fields null - Marten Deinum
When this happens it generally is an error of the user as an autowired field in Spring cannot be null . At startup...
Read more >Contexts and Dependency Injection - Quarkus
A package-private observer method. Or constructor injection: @ApplicationScoped public class CounterBean ...
Read more >What is Spring Framework? An Unorthodox Guide
Imagine you are writing a Java class that lets you access a users table in ... UserDAO has an @Autowired constructor argument →...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

It’s largely for separation of concerns. You also need to consider that
@Configurationis a specialisation of@Componentso your configuration property beans will now be picked up by component scanning. That may not be what you want.Yes. In Boot’s own codebase, our
@ConfigurationPropertiesclasses are separate from our@Configurationclasses with the former being injected into the latter as needed. The documentation also does not mix@Configurationand@ConfigurationPropertieson the same class. From what I’ve seen, most people have followed this pattern in their own code.@wilkinsona I’ve refined it a bit and pushed. I think we should consider all constructors when looking for
@Autowired, including those on user-classes if it’s a proxy.