Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ "Could not autowire." inspection, "[…] more than one bean" for Spring JPA repositories

In our code we have a number of Spring JPA repositories, one for each of our model classes. They are defined as (where <Name> is the name of our modal class):

@Repository
public interface <Name>Repository implements JpaRepository<Name, Long> {
    // …
}

We inject them in our beans using the @Injectannotation from javax:

@Inject
public void set<Name>Repository(<Name>Repository <name>Repo) {
    this.<name>Repo = <name>Repo;
}

private <Name>Repository <name>Repo;

The issue is that IntelliJ underlines the <name>Repo in the set<Name>Repository function as an error with the text:

Could not autowire. There is more than one bean of 'Repository' type. Beans: Repo, Repo.

This is only a problem with the inspection. Compilation and running our app works fine, but in the effort of making the inspections in IJ usable this is a big problem. Anyone have suggestions on how to get IntelliJ to behave?

For reference, we are using Hibernate as our JPA provider, and the data source is set up in both the Database and Persistence tool windows.

like image 695
Jonas Rabbe Avatar asked Aug 29 '14 15:08

Jonas Rabbe


People also ask

Can I autowire more than one bean?

5. Autowire Disambiguation. By default, Spring resolves @Autowired entries by type. If more than one bean of the same type is available in the container, the framework will throw a fatal exception.


2 Answers

I too have the same issue. I just commented out @Repository annotation on my Spring Data JPA repositories and everything is working fine and IntelliJ IDEA is also happy!

like image 137
K. Siva Prasad Reddy Avatar answered Oct 03 '22 11:10

K. Siva Prasad Reddy


Turns out I had 2 contexts which were picking up the same classes twice in my spring applicationContext.xml:

<mongo:repositories base-package="com.example.persistence.repositories.*"/>
...
<context:component-scan base-package="com.example.persistence.repositories.*"/>

Removing either of these lines fixed the issue.

like image 29
Babken Vardanyan Avatar answered Oct 03 '22 11:10

Babken Vardanyan