Spring boot Field required a bean of type that could not be found
Asked Answered
S

7

11

I'm going through the JPA starter tutorial for spring boot am struggling. I know the question has been asked sometimes here ('Field required a bean of type that could not be found.' error spring restful API using mongodb)

But those problems are a bit different from what I have.

Structure

java
  |
  helloWorld
  |
  web/ -- HelloWorldController
  Application
  Customer
  CustomerRepository
  ServletInitializer

As you can see all my packages related to JPA are on the same level as my Application file . According to the tutorial (https://spring.io/guides/gs/accessing-data-jpa/) this should work

My Application class

package helloWorld;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

    @Autowired
    CustomerRepository customerRepository;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

CustomerRepository

package helloWorld;


import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface CustomerRepository extends CrudRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);
}

When trying to use @Autowired I receive

***************************
APPLICATION FAILED TO START
***************************

Description:

Field customerRepository in helloWorld.Application required a bean of type 'helloWorld.CustomerRepository' that could not be found.


Action:

Consider defining a bean of type 'helloWorld.CustomerRepository' in your configuration.

Also, adding scanBasePackages={"helloWorld"}) to @SpringBootApplication does not help and from what I read it should also not be needed.

pom.xml:

<?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>helloWorld.com.example</groupId>
    <artifactId>helloWorld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</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>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-webapp-maven-plugin</artifactId>
                <version>1.1.0</version>
                <configuration>
                    <resourceGroup>maven-projects</resourceGroup>
                    <appName>${project.artifactId}-${maven.build.timestamp}</appName>
                    <region>westus</region>
                    <javaVersion>1.8</javaVersion>
                    <deploymentType>war</deploymentType>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

link to the github project

Succoth answered 4/9, 2018 at 10:51 Comment(4)
Try to annotate your CustomerRepository with @Repository.Subreption
nope - it does not work. When I download the example from the tutorial, which also does not include @Repository it works- but not on my project and I cannot find the reasonSuccoth
What is that ServletInitializer doing there. You shouldn't need it. Also you auto wire something in a configuration which itself is used to create the dependency. Why do you need to auto wire it in there.Instantaneity
Can you include the repository variable in a Controller class instead of Application and see if it works?Mandamus
B
15

You are excluding the autoconfiguration of JPA repositories. Remove the line from application.properties to let Spring make CustomerRepository a bean and configure it.

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Beccafico answered 4/9, 2018 at 11:27 Comment(1)
@AndrewTobilko great job Andrew, I learnt that. that deserved an upvoteLemcke
K
2

In my case, I just forgot to add @Component to Impl class of interface!

These errors may be due to absence of one or more stereotype annotations.

Kurgan answered 9/4, 2020 at 7:52 Comment(0)
P
2

I changed this Class Level Annotation from my Application Class.

From:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })

To:

@SpringBootApplication
Prieto answered 10/7, 2020 at 14:26 Comment(0)
L
1

Try Adding the @ComponentScan("package.path.to.your.repository") annotation above Application class.

- Just make sure your repository is in the the same package path that is written in @ComponentScan

    @SpringBootApplication
    @ComponentScan("helloworld")
    public class Application extends SpringBootServletInitializer {

         //your stuff 
    }
Lemcke answered 4/9, 2018 at 10:58 Comment(5)
I downvoted, it's unnecessary since the @SpringBootApplication class is located in the same packageBeccafico
@AndrewTobilko I think your are making a mistake brother ! try update yourself by #33620032 and dzone.com/articles/spring-spring-boot-and-component-scanLemcke
If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.Beccafico
@AndrewTobilko this is the feature of ComponentScan that you copied from javarticles.com/2016/01/… but do you see any ComponentScan in the question that you are expecting so ?! ;)Lemcke
I don't expect any ComponentScans here because both repository and spring starter are at the same package levelBeccafico
L
0

I had the same issue and everything was good except adding the below dependecies.

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.4.2</version>

And, It worked for me

Loup answered 11/1, 2021 at 14:8 Comment(0)
D
0

Reason : Spring JPA is unable to find bean/instance of CustomerRepository while compiling and starting the application.

use @Repository annotation, that will creating a bean of CustomerRepository during initialization. This annotation tells Spring to creating a bean during startup. Codewise, there will not be any syntax error, but Spring will not provide bean until it sees this annotation for e.g.

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long> 

Further ensure to annotate @Entity @Table (class level) and @Id and @Column (field level) to you Customer class, otherwise it will further throw type error.

Dorian answered 3/12, 2023 at 12:25 Comment(0)
K
-1

you need to add @Repository in repository

Kanpur answered 10/11, 2022 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.