Spring @Autowire error "more than one bean"

Answered

When a bean implementation exists both in the production and test codes, IntelliJ will mark @Autowired instances of this bean as "cannot autowire, more than one bean...".

This is of course incorrect, as the test implementation will never be deployed in a production environment.

 

In the project I'm looking at the beans are defined with @Bean annotations in classes annotated with @Configuration, but the same happens with @Services 

 

The tests and application itself run just fine, this is just an erroneous error mark in the IDE but is annoying.

Searching back in the forums this may be a regression.

 

Running the latest IntelliJ (2019.1.3) on Windows and Mac.

 

1
4 comments

Hi. Could you please provide a demo project?

0
Avatar
Permanently deleted user

Hi, please try https://github.com/igorkosandyak/spring-boot-with-hazelcast  

@Autowired
HazelcastController(HazelcastInstance hazelcastInstance) {
this.hazelcastInstance = hazelcastInstance;
}
0

Hi.

I have raised a bug report, please follow: https://youtrack.jetbrains.com/issue/IDEA-227133

As a workaround please add a qualifier as inspection suggests.

0
Avatar
Permanently deleted user

With @ConditionalOnProperty or such annotation's help , actually more than one beans exist in my application with no problem , except that intelij mark it as an error 

 

0

Please sign in to leave a comment.