[java] intellij incorrectly saying no beans of type found for autowired repository

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

No beans?

enter image description here

As you can see below it passes the test? So it must be Autowired?

enter image description here

This question is related to java spring intellij-idea annotations autowired

The answer is


Check if you missed @Service annotation in your service class, that was the case for me.


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

@SuppressWarnings("SpringJavaAutowiringInspection")

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


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

import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserServices {}

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{
/**/
}

I just had to use @EnableAutoConfiguration to address it, however this error had no functional impact.


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

@ComponentScan
public class PriceWatchTest{

    @Autowired
    private PriceWatchJpaRepository priceWatchJpaRepository;
...
...
}

Check if the package of your bean is written correctly

//Check if this is written right 
package com.package1.package2.package3


import ...

@Service
class ServiceX {

  ...

}

Surprisingly, A Feign oriented project that successfully ran with Eclipse could not run in InteliJ. When started the application, InteliJ complained about the Feign client I tried to inject to the serviceImpl layer saying: field personRestClient (my Feign client) in ... required a bean of type ... that could not be found. Consider defining a bean of type '....' in your configuration.

I wasted a long time trying to understand what is wrong. I found a solution (for InteliJ) which I do not completely understand:

  1. Alt Shift F10 (or run menu)
  2. Select 'Edit configuration'
  3. In configuration window, Check the checkbox 'include dependencies with "Provided" scope'
  4. Run your application

Or choose Eclipse :)


add the annotation @Service to your Repository class and it should work.


Use @EnableAutoConfiguration annotation with @Component at class level. It will resolve this problem.

For example:

@Component
@EnableAutoConfiguration  
public class ItemDataInitializer  {

    @Autowired
    private ItemReactiveRepository itemReactiveRepository;

    @Autowired
    private MongoOperations mongoOperations;
}

I encountered this issue too, and resolved it by the removing Spring Facet:

  • File -> Project Structure
  • Select Facets
  • Remove Spring

Good luck!


And one last piece of important information - add the ComponentScan so that the app knows about the things it needs to wire. This is not relevant in the case of this question. However if no @autowiring is being performed at all then this is likely your solution.

@Configuration
@ComponentScan(basePackages = {
    "some_package",
})
public class someService {

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


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");
    }
}

For me the solution was to place @EnableAutoConfiguration in the Application class under the @SpringBootApplication its going to underline it because its redundant. Delete it and voila all you warnings regarding missing beans are vanished! Silly Spring...


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.


Configure application context and all will be ok.

enter image description here


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


Use @AutoConfigureMockMvc for test class.


in my situation my class folder was in wrong address so check if your class is in correct package.


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.)


My version of IntelliJ IDEA Ultimate (2016.3.4 Build 163) seems to support this. The trick is that you need to have enabled the Spring Data plugin.

enter image description here


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".


I solved the problem by installing mybatis plugin in IDEA. When I installed Mybatis Plugin, it disappeared.


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!


I had a similar problem in my application. When I added annotations incorrect highliting dissapeared.

@ContextConfiguration(classes = {...})

I always solve this problem doing de following.. Settings>Inspections>Spring Core>Code than you shift from error to warning the severity option

enter image description here


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


This seems to still be a bug in the latest IntelliJ and has to do with a possible caching issue?

If you add the @Repository annotation as mk321 mentioned above, save, then remove the annotation and save again, this fixes the problem.


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!


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.

enter image description here


just add below two annotations to your POJO.

@ComponentScan
@Configuration
public class YourClass {
    //TODO
}

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 :-)


simple you have to do 2 steps

  1. add hibernate-core dependency
  2. change @Autowired to @Resource.
==>> change @Autowired to  @Resource

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to spring

Are all Spring Framework Java Configuration injection examples buggy? Two Page Login with Spring Security 3.2.x Access blocked by CORS policy: Response to preflight request doesn't pass access control check Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Spring Data JPA findOne() change to Optional how to use this? After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName The type WebMvcConfigurerAdapter is deprecated No converter found capable of converting from type to type

Examples related to intellij-idea

IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Error: Java: invalid target release: 11 - IntelliJ IDEA IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 ERROR Source option 1.5 is no longer supported. Use 1.6 or later Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 How to configure "Shorten command line" method for whole project in IntelliJ intellij idea - Error: java: invalid source release 1.9 Failed to resolve: com.google.android.gms:play-services in IntelliJ Idea with gradle

Examples related to annotations

How to inject a Map using the @Value Spring Annotation? intellij incorrectly saying no beans of type found for autowired repository @Autowired - No qualifying bean of type found for dependency Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll Can't find @Nullable inside javax.annotation.* Name attribute in @Entity and @Table Get rid of "The value for annotation attribute must be a constant expression" message @Value annotation type casting to Integer from String What does -> mean in Python function definitions? @Nullable annotation usage

Examples related to autowired

intellij incorrectly saying no beans of type found for autowired repository How do I mock an autowired @Value field in Spring with Mockito? @Autowired - No qualifying bean of type found for dependency Spring can you autowire inside an abstract class? Why is my Spring @Autowired field null? Understanding Spring @Autowired usage @Autowired and static method Injecting @Autowired private field during testing Spring expected at least 1 bean which qualifies as autowire candidate for this dependency Exclude subpackages from Spring autowiring?