intellij incorrectly saying no beans of type found for autowired repository – Autowired

Photo of author
Written By M Ibrahim
autowired azure-java-sdk hibernate-annotations intellij-idea spring

The Problem:

A Java developer is working on a Spring Boot project and using IntelliJ as the development environment. They have created a simple unit test but IntelliJ is incorrectly highlighting it as an error, marking it as ‘No beans of type found for autowired repository’. The test passes successfully, indicating that the autowiring should be working correctly. The developer is seeking a solution to resolve this error and understand why IntelliJ is displaying this incorrect message.

The Solutions:

Solution 1: Man is always greater than machine.

IntelliJ version 14.0.3 (and before) doesn’t recognize the @SpringBootApplication annotation. Hence, when using @SpringBootApplication, which internally represents @Configuration, @EnableAutoConfiguration and @ComponentScan, IntelliJ may incorrectly highlight errors related to unfulfilled @Autowire dependencies. To resolve this issue:

  1. Instead of using @SpringBootApplication, you can revert back to using @Configuration, @EnableAutoConfiguration and @ComponentScan separately.

  2. Ignore the errors, as your dependency resolution is correctly configured and your tests pass.

Remember, despite the errors, your application should run smoothly.

Always remember:

Man is always greater than machine.

Solution 2: Add `@Repository` Annotation

To fix the "no beans of type found for autowired repository" error in IntelliJ, you can add the @Repository annotation over the repository class. Even though it should work without this annotation, adding it will eliminate the error message.

Here’s the correct usage of the @Repository annotation:

@Repository
public interface YourRepository {
    // Your repository methods
}

If you’re using Spring Data with an extended Repository class, there might be a conflict in packages. To resolve this, explicitly specify the packages:

import org.springframework.data.repository.Repository;

@org.springframework.stereotype.Repository
public interface YourRepository extends Repository<YourClass, Long> {
    // Your repository methods
}

After adding the @Repository annotation, you should be able to autowire the repository without any errors:

@Autowired
YourRepository yourRepository;

This solution may not be ideal if you’re attempting to register the repository twice, but it effectively eliminates the error message in IntelliJ. It’s worth noting that newer versions of IntelliJ may have addressed this issue.

Solution 3: Specify the base packages for component scan

In the given scenario, IntelliJ highlights an error marking “no beans of type found for autowired repository” because it is unable to scan and locate the necessary components in your project. To resolve this issue, you can explicitly specify the base packages where IntelliJ should scan for components using the “@ComponentScan” annotation.

In cases where you have a Spring Boot application (which replaces the “@ComponentScan” annotation), you can use the “@SpringBootApplication” annotation instead. Here’s how you can do it:

  1. Use the “@SpringBootApplication” annotation with the “scanBasePackages” attribute in your main application class:
@SpringBootApplication(scanBasePackages = {"path.to.my.components", "path.to.my.othercomponents"})
public class MySpringBootApplication {
    // ...
}
  1. Supply the package paths where your components, including the repository classes, are located as values within the “scanBasePackages” attribute.

This approach ensures that IntelliJ can accurately scan those specific packages, detect the repository class(es), and resolve the autowiring issue. It also enables the test to pass successfully.

Solution 4: Enable the Spring Data plugin

To resolve the issue of IntelliJ incorrectly flagging an error for a unit test due to no beans of type found for autowired repository, you can enable the Spring Data plugin in IntelliJ.

Here’s a step-by-step guide:

  1. Open IntelliJ IDEA and go to “Settings” or “Preferences” (depending on your operating system).
  2. In the search bar, type “Plugins” and click on the “Plugins” tab.
  3. In the list of plugins, search for “Spring Data”.
  4. If the plugin is not already installed, click the “Install” button.
  5. Once the plugin is installed, restart IntelliJ IDEA.

After enabling the Spring Data plugin, IntelliJ should correctly recognize the autowired repository and the unit test should pass without errors.

Solution 5: Change Severity Level of IDE Inspection

Steps:

  1. Open your IntelliJ IDEA settings by clicking on "File" -> "Settings" (or "Preferences" on macOS).

  2. Navigate to "Inspections" -> "Spring Core" -> "Code".

  3. Locate the inspection rule "Autowiring for Bean Not Found" or similar.

  4. Change the severity level from "Error" to "Warning" or "Information".

  5. Click "Apply" and then "OK" to save your changes.

Explanation:

The "Autowiring for Bean Not Found" inspection is designed to warn you about potential problems with autowiring in your Spring application. However, in certain situations, this inspection can produce false positives, especially during unit testing.

By changing the severity level of the inspection to "Warning" or "Information", you can prevent IntelliJ IDEA from marking your unit tests as errors due to this false positive. This will allow you to focus on the actual issues in your code without being distracted by these false errors.

This solution is relatively simple and straightforward, and it allows you to continue using IntelliJ IDEA’s autowiring inspection while avoiding the false positive errors in your unit tests.