question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ConfigurationProperties constructor @Autowired detection fails for proxy classes

See original GitHub issue

Spring 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:closed
  • Created 10 months ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Nov 8, 2022

Is the recommendation just for separation of concerns? Or are there other negative consequences?

It’s largely for separation of concerns. You also need to consider that @Configuration is a specialisation of @Component so your configuration property beans will now be picked up by component scanning. That may not be what you want.

In your experience is it more common that the @ConfigurationProperties class/bean is injected into the bean that needs it rather than passing the fields to it inside of the @Bean method?

Yes. In Boot’s own codebase, our @ConfigurationProperties classes are separate from our @Configuration classes with the former being injected into the latter as needed. The documentation also does not mix @Configuration and @ConfigurationProperties on the same class. From what I’ve seen, most people have followed this pattern in their own code.

0reactions
philwebbcommented, Nov 10, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found