intellij incorrectly saying no beans of type found for autowired repository

前端 未结 30 2760
北恋
北恋 2020-11-30 18:33

I have created a simple unit test but IntelliJ is incorrectly highlighting it red. marking it as an error

No beans?

相关标签:
30条回答
  • 2020-11-30 18:49

    I am using this annotation to hide this error when it appears in IntelliJ v.14:

    @SuppressWarnings("SpringJavaAutowiringInspection")
    
    0 讨论(0)
  • 2020-11-30 18:50

    Configure application context and all will be ok.

    0 讨论(0)
  • 2020-11-30 18:51

    in my Case, the Directory I was trying to @Autowired was not at the same level,

    after setting it up at the same structure level, the error disappeared

    hope it can helps some one!

    0 讨论(0)
  • 2020-11-30 18:52

    Add Spring annotation @Repository over the repository class.

    I know it should work without this annotation. But if you add this, IntelliJ will not show error.

    @Repository
    public interface YourRepository ...
    ...
    

    If you use Spring Data with extending Repository class it will be conflict pagkages. Then you must indicate explicity pagkages.

    import org.springframework.data.repository.Repository;
    ...
    
    @org.springframework.stereotype.Repository
    public interface YourRepository extends Repository<YourClass, Long> {
        ...
    }
    

    And next you can autowired your repository without errors.

    @Autowired
    YourRepository yourRepository;
    

    It probably is not a good solution (I guess you are trying to register repositorium twice). But work for me and don't show errors.

    Maybe in the new version of IntelliJ can be fixed: https://youtrack.jetbrains.com/issue/IDEA-137023

    0 讨论(0)
  • 2020-11-30 18:52

    My solution to this issue in my spring boot application was to open the spring application context and adding the class for the missing autowired bean manually!

    (access via Project Structure menu or spring tool window... edit "Spring Application Context")

    So instead of SpringApplicationContext just containing my ExampleApplication spring configuration it also contains the missing Bean:

    SpringApplicationContext:

    • ExampleApplication.java
    • MissingBeanClass.java

    et voilà: The error message disappeared!

    0 讨论(0)
  • 2020-11-30 18:54

    It can be solved by placing @EnableAutoConfiguration on spring boot application main class.

    0 讨论(0)
提交回复
热议问题