profile
viewpoint

Ask questionsImprove diagnostics when configuration property scanning and component scanning both create a bean for the same class

Problem After upgrading Spring Boot to version 2.2.0.M2 (from 2.2.0.M1) beans annotated with @ConfigurationProperties are loaded twice causing application to fail with following exception -

Parameter 0 of constructor in config.HazelcastConfiguration required a single bean, but 2 were found:
	- hazelcastProperties: defined in file [D:\LocationGuru\Projects\JavaEE\spring-boot-issues\target\classes\config\HazelcastProperties.class]
	- application.hazelcast-config.HazelcastProperties: defined in null

See application log below -

2019-04-16 20:40:51.517  INFO 14804 --- [           main] config.HazelcastProperties               : Creating bean HazelcastProperties ..
2019-04-16 20:40:51.517  INFO 14804 --- [           main] config.HazelcastProperties               : Creating bean HazelcastProperties ..
2019-04-16 20:40:51.517  INFO 14804 --- [           main] config.HazelcastConfiguration            : Found 2 HazelcastProperties beans ..

Workaround Adding @Primary to all the classes which are annotated with @ConfigurationProperties seems to resolve the issue temporarily.

Observation 1 With proposed workaround applied autowiring list of instances (as below) makes Spring Boot to load the beans twice -

@Autowired
public HazelcastConfiguration(final List<HazelcastProperties> propertiesList)
{
    logger.info("Found {} HazelcastProperties beans ..", propertiesList.size());
}

Observation 2 With proposed workaround applied autowiring single instances (as below) makes Spring Boot to load the beans once (as expected) -

@Autowired
public HazelcastConfiguration(final HazelcastProperties properties)
{
    logger.info("Found 1 HazelcastProperties beans ..");
}

Please see attached project (spring-boot-issues.zip) for reproducing the issue.

Might be related to gh-15802.

spring-projects/spring-boot

Answer questions Polve

In the short term removing @component will give you a single bean for your configuration properties class

After a big refactor I was able to create separate classes with only @ConfigurationProperties annotations, but this becomes incompatible with 2.1 because with it I need to mark them also with @Component.

I wrote the details in #16580 and #16969

useful!

Related questions

Dependency resolution fails with Gradle 5.3.x to 5.6.x hot 4
get error messag Could not fetch the SequenceInformation from the database, hibernate_sequence doesn't exist hot 2
[spring-boot 2.3.0] ResponseStatusException reason is ignored - spring-boot hot 2
Controller annotated with @Timed and active TimedAspect clashes with WebMvcMetricsFilter hot 2
Source file must be provided failure when running spring-boot:repackage from the command-line hot 2
Actuator: NPE in LongTaskTimingHandlerInterceptor hot 2
ConfigurationProperties with constructor binding cannot be mocked hot 2
NoClassDefFoundError: net/bytebuddy/NamingStrategy$SuffixingRandom$BaseNameResolver hot 2
No bean named &#39;entityManagerFactory&#39; available hot 2
Bug in org.springframework.boot on 2.2.3.RELEASE version hot 2
"factory already defined" error with spring-boot-maven-plugin in maven multi-module application hot 2
Deadlock between BackgroundPreinitializer and main thread in Spring Cloud Config Server hot 2
spring boot Servlet.service() for servlet [dispatcherServlet] in context with path threw exception hot 2
Migrating OAuth2 from Spring Boot 1.5 to 2.0 Broken hot 2
Add AutoConfiguration support for spring-security-saml2-service-provider hot 2
Github User Rank List