intellij incorrectly saying no beans of type found for autowired repository

前端 未结 30 2763
北恋
北恋 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 19:07

    If you don't want to make any change to you code just to make your IDE happy. I have solved it by adding all components to the Spring facet.

    1. Create a group with name "Service, Processors and Routers" or any name you like;
    2. Remove and recreate "Spring Application Context" use the group you created previously as a parent.

    0 讨论(0)
  • 2020-11-30 19:07

    Sometimes - in my case that is - the reason is a wrong import. I accidentally imported

    import org.jvnet.hk2.annotations.Service
    

    instead of

    import org.springframework.stereotype.Service
    

    by blindly accepting the first choice in Idea's suggested imports. Took me a few minutes the first time it happend :-)

    0 讨论(0)
  • 2020-11-30 19:07

    All you need to do to make this work is the following code:

    @ComponentScan
    public class PriceWatchTest{
    
        @Autowired
        private PriceWatchJpaRepository priceWatchJpaRepository;
    ...
    ...
    }
    
    0 讨论(0)
  • 2020-11-30 19:08

    I had this same issue when creating a Spring Boot application using their @SpringBootApplication annotation. This annotation represents @Configuration, @EnableAutoConfiguration and @ComponentScan according to the spring reference.

    As expected, the new annotation worked properly and my application ran smoothly but, Intellij kept complaining about unfulfilled @Autowire dependencies. As soon as I changed back to using @Configuration, @EnableAutoConfiguration and @ComponentScan separately, the errors ceased. It seems Intellij 14.0.3 (and most likely, earlier versions too) is not yet configured to recognise the @SpringBootApplication annotation.

    For now, if the errors disturb you that much, then revert back to those three separate annotations. Otherwise, ignore Intellij...your dependency resolution is correctly configured, since your test passes.

    Always remember...

    Man is always greater than machine.

    0 讨论(0)
  • 2020-11-30 19:08

    Sometimes you are required to indicate where @ComponentScan should scan for components. You can do so by passing the packages as parameter of this annotation, e.g:

    @ComponentScan(basePackages={"path.to.my.components","path.to.my.othercomponents"})
    

    However, as already mentioned, @SpringBootApplication annotation replaces @ComponentScan, hence in such cases you must do the same:

    @SpringBootApplication(scanBasePackages={"path.to.my.components","path.to.my.othercomponents"})
    

    At least in my case, Intellij stopped complaining.

    0 讨论(0)
  • 2020-11-30 19:08

    What you need to do is add

    @ComponentScan("package/include/your/annotation/component") in AppConfiguration.java.

    Since I think your AppConfiguraion.java's package is deeper than your annotation component (@Service, @Component...)'s package,

    such as "package/include/your/annotation/component/deeper/config".

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