Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression of erroneous implicit constructor binding for @ConfigurationProperties between 3.0.5 and 3.1.1 #36412

Closed
GrantFleming opened this issue Jul 14, 2023 · 1 comment
Labels
status: duplicate A duplicate of another issue

Comments

@GrantFleming
Copy link

GrantFleming commented Jul 14, 2023

There were issues previously where spring would attempt constructor binding of @ConfigurationProperties under some circumstances where it should not.

Related to issues #33710 #33509

Issue seems to have surfaced in 3.0.0 and seems to have been fixed in 3.0.2, and remains fixed through to 3.0.5 inclusive.

In 3.1.1 this behaviour seems to have regressed.

Can be reproduced with the following kotlin example:

@SpringBootApplication
class ConstructorbindingApplication

fun main(args: Array<String>) {
	runApplication<ConstructorbindingApplication>(*args)
}

@Component
@ConfigurationProperties("top-level-property")
class ApplicationProperties(
	var someProperty: String = ""
) {}

Application runs successfully in 3.0.5, however in 3.1.1 throws the following exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationProperties' defined in file [C:\Users\grant\Desktop\constructorbinding\constructorbinding\target\classes\com\example\constructorbinding\ApplicationProperties.class]: Cannot bind @ConfigurationProperties for bean 'applicationProperties'. Ensure that @ConstructorBinding has not been applied to regular bean
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:605) ~[spring-beans-6.0.10.jar:6.0.10]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.10.jar:6.0.10]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.10.jar:6.0.10]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.10.jar:6.0.10]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.10.jar:6.0.10]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.10.jar:6.0.10]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.10.jar:6.0.10]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941) ~[spring-context-6.0.10.jar:6.0.10]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.10.jar:6.0.10]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.1.jar:3.1.1]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.1.jar:3.1.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.1.jar:3.1.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.1.jar:3.1.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.1.jar:3.1.1]
	at com.example.constructorbinding.ConstructorbindingApplicationKt.main(ConstructorbindingApplication.kt:23) ~[classes/:na]
Caused by: java.lang.IllegalStateException: Cannot bind @ConfigurationProperties for bean 'applicationProperties'. Ensure that @ConstructorBinding has not been applied to regular bean
	at org.springframework.util.Assert.state(Assert.java:76) ~[spring-core-6.0.10.jar:6.0.10]
	at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.bind(ConfigurationPropertiesBindingPostProcessor.java:92) ~[spring-boot-3.1.1.jar:3.1.1]
	at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:79) ~[spring-boot-3.1.1.jar:3.1.1]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:419) ~[spring-beans-6.0.10.jar:6.0.10]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1762) ~[spring-beans-6.0.10.jar:6.0.10]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.10.jar:6.0.10]
	... 14 common frames omitted

The author of one of the original issues has also noted the regression on the now-closed issue.

Workarounds

  1. Provide a default @Autowired no-arg constructor:
@Component
@ConfigurationProperties("top-level-property")
class ApplicationProperties(
	var someProperty: String = ""
) {
	@Autowired
	constructor() : this("")
}
  1. Annotate the class with @configuration rather than @component:
@Configuration
@ConfigurationProperties("top-level-property")
class ApplicationProperties(
	var someProperty: String = ""
) {
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 14, 2023
@wilkinsona
Copy link
Member

Thanks for the report. The problem does not occur with 3.0.9-SNAPSHOT or 3.1.2-SNAPSHOT (available from https://repo.spring.io/snapshot). It has already been fixed by the changes made for #36175.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale Jul 14, 2023
@wilkinsona wilkinsona added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants