14

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.

Jonas Rabbe
  • 2,175
  • 1
  • 16
  • 14
  • Does IntelliJ tell you which beans it thinks match the injection? – geoand Aug 29 '14 at 16:17
  • 1
    Also on a different note, you don't need the `@Repository` annotation. Spring Data JPA will create the bean automatically if it's configured correctly – geoand Aug 29 '14 at 16:22
  • Yes, @geoand, the beans that match are `Repo` and `Repo`. Not the most helpful. Only thing I could think of was that IJ picked up both the `@Repository` annotation, and the spring config? – Jonas Rabbe Sep 01 '14 at 01:40
  • That's a possibility – geoand Sep 01 '14 at 04:43
  • If you think that this is indeed a problem with IntelliJ you should report it to them as well. I don’t think there’s anything wrong in asking, but if IntelliJ is aware of the issue it can get properly solved in future versions. – Alf Jun 18 '15 at 15:51

2 Answers2

12

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!

K. Siva Prasad Reddy
  • 11,786
  • 12
  • 68
  • 95
  • 1
    Turns out this was pretty much what was happening. We used the `@Repository` annotations along with a Java-based configuration which also manually initialized all our repositories, hence IJ thought there were two identical beans. However, because we did not configure Spring to looks for the `@Repository` annotation, but only used the manual config when running the app it wasn't causing problems. Removing the `@Repository` annotation did the trick, and is correct since we were not using it. – Jonas Rabbe Oct 29 '15 at 14:40
  • One issue I have is that if I use Spring Boot, currently IntelliJ v 15 complains incorrectly that it cannot autowire any of the Spring Data repositories (such as CrudRepository) hence adding @Repository fixes this. However this then causes an error if you don't use Spring Boot ( I have a project with a shared Dao Library using a standard spring app and spring boot! - what fixes one breaks another) .However there is no runtime error and all works as expected – ismoore999 Feb 23 '16 at 09:44
1

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.

Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87