Skip to content

Misleading error message with immutable configuration properties and @Component #18646

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

Closed
snicoll opened this issue Oct 18, 2019 · 7 comments
Closed
Labels
status: superseded An issue that has been superseded by another type: task A general task

Comments

@snicoll
Copy link
Member

snicoll commented Oct 18, 2019

Consider the following example that is target for regular classpath scanning

@Component
@ConstructorBinding
@ConfigurationProperties("brol")
public class BrolProperties {

	private final String endpoint;

	BrolProperties(String endpoint) {
		this.endpoint = endpoint;
	}

This fails with the following exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'brolProperties': @EnableConfigurationProperties or @ConfigurationPropertiesScan must be used to add @ConstructorBinding type sample.service.BrolProperties
	at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.validate(ConfigurationPropertiesBeanDefinitionValidator.java:60) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
	at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.postProcessBeanFactory(ConfigurationPropertiesBeanDefinitionValidator.java:45) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:174) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
	at sample.service.ServiceApplication.main(ServiceApplication.java:35) [classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.0.RELEASE.jar:2.2.0.RELEASE]

Removing @Component fixes the issue but I think the error message could be reworded to make that a bit more explicit.

@snicoll snicoll added the type: task A general task label Oct 18, 2019
@snicoll snicoll added this to the 2.2.1 milestone Oct 18, 2019
@philwebb philwebb modified the milestones: 2.2.1, 2.2.x Oct 23, 2019
@onobc
Copy link
Contributor

onobc commented Oct 30, 2019

@snicoll you mind if I grab this one?

@snicoll
Copy link
Member Author

snicoll commented Oct 30, 2019

Thanks for the offer but we're reconsidering this mechanism (see #18652) and this may have consequences on this one.

@onobc
Copy link
Contributor

onobc commented Oct 30, 2019

Yeh I saw quite a few other similar issues for the new ConfigurationProperties binding and thought that may be the case. I will keep looking for other candidates.

@checketts
Copy link
Contributor

@snicoll Thanks for the example of the failing application and the explanation of removing @Component.

I ran into the same error message when I had an @Import of my ConfigurationProperties class: @Import({BrolProperties.class})

Removing the @Import also fixed the problem.

@eduanb
Copy link

eduanb commented Jul 21, 2020

Ran into this issue today. When specifying the class with @ConfigurationProperties in org.springframework.boot.autoconfigure.EnableAutoConfiguration= in spring.factories and having @ConfigurationPropertiesScan you get the same misleading error message.

@wilkinsona
Copy link
Member

wilkinsona commented Jul 22, 2020

The changes made for #20798 have changed the error message as failure analysis is now performed. It is now the following:

***************************
APPLICATION FAILED TO START
***************************

Description:

BrolProperties is annotated with @ConstructorBinding but it is defined as a regular bean which caused dependency injection to fail.

Action:

Update your configuration so that BrolProperties is defined via @ConfigurationPropertiesScan or @EnableConfigurationProperties.

It's difficult to be much more specific as, while a class may be annotated with @Component, that isn't enough to be certain that it was registered by component scanning.

WDYT, @snicoll? Can we close this one or should we use it to try to further refine the improvements made in #20798?

@snicoll
Copy link
Member Author

snicoll commented Jul 22, 2020

Thanks for the follow-up. Yep, all good, closing this one as being superseded by #20798.

@snicoll snicoll closed this as completed Jul 22, 2020
@snicoll snicoll removed this from the 2.2.x milestone Jul 22, 2020
@snicoll snicoll added the status: superseded An issue that has been superseded by another label Jul 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another type: task A general task
Projects
None yet
Development

No branches or pull requests

6 participants