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

Controller annotated with @Timed and active TimedAspect clashes with WebMvcMetricsFilter hot 2
Actuator: NPE in LongTaskTimingHandlerInterceptor 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
Classloading problems with Spring Boot, JDK11 and Security Manager hot 1
java.lang.NullPointerException: null occurs in HttpExchangeTracer hot 1
Multiple data source projects cannot be started hot 1
Dependency resolution fails with Gradle 5.3.x to 5.6.x hot 1
get error messag Could not fetch the SequenceInformation from the database, hibernate_sequence doesn't exist hot 1
HttpTraceFilter bean missing hot 1
A java.lang.NoClassDefFoundError: ch/qos/logback/classic/spi/ThrowableProxy was thrown when killing my app hot 1
Source file must be provided failure when running spring-boot:repackage from the command-line hot 1
Issue with spring boot v2.1.2 with DocumentBuilderFactory.setFeature(String) hot 1
ErrorPageFilter ignores REST response in case of Exception hot 1
ConfigurationProperties with constructor binding cannot be mocked hot 1
Github User Rank List