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

4
  • Try to annotate your CustomerRepository with @Repository.
    – BigJ
    Commented Sep 4, 2018 at 11:01
  • 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 reason
    – xhallix
    Commented Sep 4, 2018 at 11:03
  • 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.
    – M. Deinum
    Commented Sep 4, 2018 at 11:03
  • Can you include the repository variable in a Controller class instead of Application and see if it works? Commented Sep 4, 2018 at 11:08

7 Answers 7

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
1
  • 1
    @AndrewTobilko great job Andrew, I learnt that. that deserved an upvote Commented Sep 5, 2018 at 19:04
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.

0
2

I changed this Class Level Annotation from my Application Class.

From:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })

To:

@SpringBootApplication
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 
    }
5
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

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.

-1

you need to add @Repository in repository

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.