intellij incorrectly saying no beans of type found for autowired repository

前端 未结 30 2765
北恋
北恋 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:01

    IntelliJ IDEA Ultimate

    Add your main class to IntelliJ Spring Application Context, for example Application.java

    File -> Project Structure..

    left side: Project Setting -> Modules

    right side: find in your package structure Spring and add + Application.java

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

    I had similar issue in Spring Boot application. The application utilizes Feign (HTTP client synthetizing requests from annotated interfaces). Having interface SomeClient annotated with @FeignClient, Feign generates runtime proxy class implementing this interface. When some Spring component tries to autowire bean of type SomeClient, Idea complains no bean of type SomeClient found since no real class actually exists in project and Idea is not taught to understand @FeignClient annotation in any way.

    Solution: annotate interface SomeClient with @Component. (In our case, we don't use @FeignClient annotation on SomeClient directly, we rather use metaannotation @OurProjectFeignClient which is annotated @FeignClient and adding @Component annotation to it works as well.)

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

    As long as your tests are passing you are good, hit alt + enter by taking the cursor over the error and inside the submenu of the first item you will find Disable Inspection select that

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

    Putting @Component or @configuration in your bean config file seems to work, ie something like:

    @Configuration
    public class MyApplicationContext {
        @Bean
        public DirectoryScanner scanner() {
            return new WatchServiceDirectoryScanner("/tmp/myDir");
        }
    }
    
    @Component
    public class MyApplicationContext {
        @Bean
        public DirectoryScanner scanner() {
            return new WatchServiceDirectoryScanner("/tmp/myDir");
        }
    }
    
    0 讨论(0)
  • 2020-11-30 19:06

    Have you checked that you have used @services annotation on top of your service implementation? It worked for me.

    import org.springframework.stereotype.Service;
    
    @Service
    public class UserServiceImpl implements UserServices {}
    
    0 讨论(0)
  • 2020-11-30 19:07

    I am using spring-boot 2.0, and intellij 2018.1.1 ultimate edition and I faced the same issue.

    I solved by placing @EnableAutoConfiguration in the main application class

    @SpringBootApplication
    @EnableAutoConfiguration
    class App{
    /**/
    }
    
    0 讨论(0)
提交回复
热议问题