"Could not autowire. No beans of type... found" with Simple project
Asked Answered
M

6

25

I downloaded the simple JPA Spring Boot tutorial and it worked just fine. However, when I attempt to replicate this simple behavior in my own test project, I get a "could not autowire" error on the bean injection in my Application.demo() method that returns a CommandLineRunner. The project is so barebones I don't even know what to submit but here's the POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>test</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

And the application.

package com.example;

@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    @Bean
    //errors with: "Could not autowire. No beans of 'SimpRepository' type found"
    public CommandLineRunner demo(SimpRepository repository) {
        return (args) -> {

        };
    }
}

And the Repository service:

package com.example;
public interface SimpRepository extends CrudRepository<Simp, Long> {

}

for the following entity:

package com.example;
@Entity
public class Simp {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String value;

    public Simp(String value) {
        this.value = value;
    }
}
Midpoint answered 9/10, 2016 at 4:2 Comment(5)
You need to post the error, not just paraphrase it. You also need to specify the packages all your classes are in.Sulfonate
Can you try with @Repository on top of your SimpRepository interface.Swaim
@Swaim Adding the annotation does fix the problem. But I am curious as to why it works in the original demo project without itMidpoint
Answer is already provided by metacubed . You can upvote comment if it helped you in anyway.Swaim
The example works fine for me (I made a new project and copied your code). The only issue I'm noticing is that you should have a default no-arg constructor for your entity, but even without a constructor the repository is injectable. Make sure all classes are within the same package (or a subpackage of TestApplication) and make sure you've built your code properly.Doting
Q
23

A couple of possibilities here.

You need to add the @EnableJpaRepositories(basePackages = {"your.pkg.here"}) to the TestApplication. This tells Spring Data to look for your repository classes under the specified package. If the repository package name is the same as the TestApplication, you can skip the basePackages part.

Similarly, if your TestApplication and SimpRepository are not in the same package, you need to add a @ComponentScan with the list of all relevant packages.

Quietly answered 9/10, 2016 at 7:25 Comment(2)
This sunds reasonable, and for the record they are in the same package, but I'm trying to figure out why it works in the Spring Demo project I posted a link to, but doesn't work for the project I posted. Both seem to be functionally identical but I know there must be some subtle difference between the two. The demo project's files are in the same package as well, and there is no reference to EnableJpaRepositories thereMidpoint
@Midpoint hmm, that tutorial explicitly says that you need a @EnableJpaRepositories only if your package is different. However, I have always needed to add the annotation regardless.Quietly
P
4

I missed a simple @Component for the class. This could be one basic problem.

Ppm answered 17/6, 2020 at 7:46 Comment(0)
J
3

Add a simple @Repository annotation above your repository class and it will work fine.

Jill answered 13/3, 2021 at 22:8 Comment(0)
D
0

I have a similar issue IDEA-271551 just opened with JetBrains. Please upvote it.

In my case I resolved(not actual resolution,but workaround) it by explicitly annotating my repository interface with @Repository stereotype.

However, I also noticed that later if I again remove the annotation the IDE no longer complains.

Strange, but it did happen in my case.

Distil answered 15/6, 2021 at 9:10 Comment(1)
I also faced the same issue and upvotedPersse
A
0

I forgot to add " annotationProcessor <<<-libs.mapstructprocessor->>> " among Dependencies in build.gradle. It was fixed when I added it.

  dependencies {
    
    implementation  libs.mapstruct
    implementation  libs.mapstructprocessor
    implementation  libs.lombok

    annotationProcessor libs.lombok
    annotationProcessor libs.mapstructprocessor
}
Arnaud answered 4/2, 2022 at 11:52 Comment(0)
T
0

I had a quite a flat package structure (notice that I don't have the usual com.x.y.z in my application) hence met this issue. Following @ComponentScan fixed the issue:

@SpringBootApplication
@ComponentScan(basePackages = {"services", "views", "database"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

You don't need this explicit definitions if your Spring Application class too is in the same package hierarchy.

Trouveur answered 28/10, 2023 at 22:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.