10

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

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
xhallix
  • 2,919
  • 5
  • 37
  • 55
  • Try to annotate your CustomerRepository with @Repository. – BigJ Sep 04 '18 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 Sep 04 '18 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 Sep 04 '18 at 11:03
  • Can you include the repository variable in a `Controller` class instead of `Application` and see if it works? – pkgajulapalli Sep 04 '18 at 11:08

6 Answers6

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
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
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.

Demobilizer
  • 663
  • 9
  • 12
2

I changed this Class Level Annotation from my Application Class.

From:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })

To:

@SpringBootApplication
javaPlease42
  • 4,699
  • 7
  • 36
  • 65
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 
    }
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • 2
    I downvoted, it's unnecessary since the `@SpringBootApplication` class is located in the same package – Andrew Tobilko Sep 04 '18 at 10:59
  • @AndrewTobilko I think your are making a mistake brother ! try update yourself by https://stackoverflow.com/questions/33619532/configuration-using-annotation-springbootapplication and https://dzone.com/articles/spring-spring-boot-and-component-scan – MohammadReza Alagheband Sep 04 '18 at 11:03
  • If specific packages are not defined, scanning will occur from the package of the class that declares this annotation. – Andrew Tobilko Sep 04 '18 at 11:14
  • @AndrewTobilko this is the feature of ComponentScan that you copied from http://www.javarticles.com/2016/01/spring-componentscan-annotation-example.html but do you see any ComponentScan in the question that you are expecting so ?! ;) – MohammadReza Alagheband Sep 04 '18 at 11:17
  • 1
    I don't expect any `ComponentScan`s here because both repository and spring starter are at the same package level – Andrew Tobilko Sep 04 '18 at 11:20
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

-1

you need to add @Repository in repository