Could not autowire. No beans of `Repository' type found

15,400

Solution 1

Note this is for IntelliJ IDEA 2018.3.3 Ultimate Edition (But should work for other versions too)

I've noticed that this errors occurs (at least in my projects) when a configuration uses @ComponentScan and thus loads another class annotated with @Configuration. IntelliJ seems to not fully recognize it which leads to the error/warning:

  1. Click in the file structure on the top folder
  2. Press F4
  3. Go to "Modules"
  4. Select the module in which IntelliJ complains from the list
  5. Click on "Spring"
  6. Click on the plus icon
  7. Select the configuration which provides your xRepository
  8. Save

Solution 2

It is possible that Intellij has trouble with your package name in @EnableJpaRepositories annotation. Can you try putting that annotation in your main Spring class without any basePackages property defined?

@EnableJpaRepositories(entityManagerFactoryRef = "xxx", transactionManagerRef = "xxx")
@SpringBootApplication
public class FooApplication {

    public static void main(String[] args) {
        SpringApplication.run(FooApplication.class, args);
    }
}

Your main Spring class should be in the root of course;

root
  ServicePackage
    MyService
  RepositoryPackage
    MyRepository
  FooApplication <---- main class here

Solution 3

Try it :

@Configuration
@ComponentScan(basePackages = {"your repository package}")
Share:
15,400
xingbin
Author by

xingbin

Updated on June 07, 2022

Comments

  • xingbin
    xingbin almost 2 years

    I'm using Spring Data Jpa, this is my project structure:

    App
      ConfigPackage
        MyConfig
      ServicePackage
        MyService
      RepositoryPackage
        MyRepository
    

    Here is the MyRepository:

    public interface MyRepository extends JpaRepository<MyEntity, Long> {
    }
    

    Here is the MyService:

    @Service
    public class MyService {
    
        @Autowired
        private MyRepository myRepository; <---here
    
        ...
    }
    

    Here is the MyConfig:

    @Configuration
    @EnableJpaRepositories(
            basePackages = "RepositoryPackage",
            entityManagerFactoryRef = "xxx",
            transactionManagerRef = "xxx"
    )
    public class MyConfig {
    }
    

    I use @Autowired to inject MyRepository to MyService, but IntelliJ always complains

    Could not autowire. No beans of 'MyRepository' type found

    even if the code can compile and run successfully.

    Why can not IntelliJ recognize this is not an error? How to remove the warning of IntelliJ?

    IntelliJ Version: 2018.2.6

  • xingbin
    xingbin over 5 years
    Thanks for the reply. I did that, but still the warning...
  • xingbin
    xingbin over 5 years
    Where should I put this annotation on?
  • xingbin
    xingbin over 5 years
    Thanks, I'll try it. I can not figure out why IntelliJ mistake it as an error.
  • AlgorithmFromHell
    AlgorithmFromHell over 5 years
    Put it above the field. It should work just as well if you put it on top of the class, but then you're going to suppress warnings for all fields.
  • xingbin
    xingbin over 5 years
    I've tried both.. No luck... Still the warning.
  • xingbin
    xingbin over 5 years
    No luck, it still warns...
  • Lino
    Lino over 5 years
    @竹杖芒鞋轻胜马 does the [alt] + [enter] menu provide any solutions? You can also ignore those warnings by going to Settings > Editor > Inspections and turning it off
  • xingbin
    xingbin over 5 years
    Thanks for your reply. Use @Repository could elimate the warning, but JpaRepository is NoRepositoryBean. I think it should not be annotated with@Repository. My main class is App, so I suppose the repository class should be scanned.
  • xingbin
    xingbin over 5 years
    I already did that in MyConfig
  • buræquete
    buræquete over 5 years
    @竹杖芒鞋轻胜马 can you put the @EnableJpaRepositories on the App class then? Without basePackages property? That would resolve I'd hope!
  • xingbin
    xingbin over 5 years
    Seems does not help
  • xingbin
    xingbin over 5 years
    Yes, it let me add @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspec‌​tion"), and the warnings disappear. I do not know why...
  • buræquete
    buræquete over 5 years
    @竹杖芒鞋轻胜马 How about adding the facet to your project? From Project Structure menu, go to your project/module and add JPA facet? example
  • xingbin
    xingbin over 5 years
    Thanks you sir, I already did that. But IntelliJ seems just can not recognize it.
  • buræquete
    buræquete over 5 years
    @竹杖芒鞋轻胜马 really interesting... It might be that your intellij has trouble, did you try following the steps here ? Otherwise I have no other idea sadly :'(
  • xingbin
    xingbin over 5 years
    Thanks again. I will try it tomorrow.
  • buræquete
    buræquete over 5 years
    @竹杖芒鞋轻胜马 I have one question, when you type methods in a repository, does Intellij complete them? Like when you type findBy, does it give suggestions?
  • xingbin
    xingbin over 5 years
    Yes, it gives me auto complete. So I'm pretty sure IntelliJ can scan the repository. And the warning does no harm, I just don't like warnings.
  • buræquete
    buræquete over 5 years
    @竹杖芒鞋轻胜马 yeah I understand, I had the exact case, no problems but annoying warnings, not sure what stopped them though...
  • buræquete
    buræquete over 5 years
    @竹杖芒鞋轻胜马 use autowiring via constructors? That would make those warnings disappear I think, just follow this This is what I did I think, that removed the warnings.
  • xingbin
    xingbin over 5 years
    Adding @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspec‌​tion") solved the problem. But when I google SpringJavaInjectionPointsAutowiringInspection, I did not find any doc or official documents...
  • xingbin
    xingbin over 5 years
    Yes, I'm autowiring via constructors... While the warning shows whether filed or constructor injection..
  • Lino
    Lino over 5 years
    @竹杖芒鞋轻胜马 I also found this question: stackoverflow.com/questions/21323309/…, what happens when you remove the spring facet in your project configuration and let IntelliJ detect it?
  • xingbin
    xingbin over 5 years
    The warning disappears after I removing the Spring facet. While, I'm afraid this brings another problem, even if I remove the annotations on the other classes, such as @Service, IntelliJ now can not detect it, so this might disable the whole detect function of IntelliJ...
  • Lino
    Lino over 5 years
    @竹杖芒鞋轻胜马 Try to click on [Build] and then [Rebuild Project] in the top bar, this sometimes triggers intelliJ to scan everything, and as it says just completely rebuild your project
  • xingbin
    xingbin over 5 years
    Seems there is a solution here stackoverflow.com/a/42025703/6690200
  • xingbin
    xingbin over 5 years
    The solution I take before, use @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspec‌​tion"), was suggested by IntelliJ after I enter [alt] + [enter]. It does work but I will not use it because I do not want to import this annotation. I will enable spring data plugin, like another answer said.
  • Eugene Kortov
    Eugene Kortov about 3 years
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspec‌​tion")