Search code examples
javaspring-bootjpaspring-boot-jpa

I am getting NoSuchBeanDefinitionexception while creating a simple Spring Boot project with JpaRepository


ScreenShot of My Project Structure from STS

I am creating a simple Spring Boot application with JpaRepository, but when I am trying to run my application it gives an error that "NoSuchBeanDefinitionException". I am new to Spring Boot.

I have also tried to annotate my main class with @SpringBootApplication @EnableJpaRepositories("com.ab.repository") annotations but whenever I am trying to annotate @EnableJpaRepository(), it is showing error in STS that

The type org.springframework.data.repository.config.BootstrapMode cannot be resolved. It is indirectly referenced from required .class files.

Previously I was not using this annotation but I saw in a question that I have to tell my class to enable JPA repository, so I tried this as well, but it is also not working.

My main class


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootMain {

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

}

Controller Class is :


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ab.model.WebServiceModel;
import com.ab.service.WebSrvService;

@RestController
public class WebServiceController {

    @Autowired
    private WebSrvService webSrvService;

    @PostMapping(value = "/save")
    public void saveRecord(@RequestBody WebServiceModel webServiceModel) {
        webSrvService.saveRecord(webServiceModel);
    }
}

Service class:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ab.model.WebServiceModel;
import com.ab.repository.WebServiceRepository;

@Service
public class WebSrvService {
    @Autowired
    private WebServiceRepository webServiceRepository;

    public void saveRecord(WebServiceModel webServiceModel) {
        webServiceRepository.save(webServiceModel);
    }
}

Repository Interface:


import org.springframework.data.jpa.repository.JpaRepository;

import com.ab.model.WebServiceModel;

public interface WebServiceRepository extends JpaRepository<WebServiceModel, Integer> {

}

and my pom.xml file is:

<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.ab</groupId>
    <artifactId>SpringBootTry</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.1.10.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.10.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.2.18.RELEASE</version>
        </dependency>
</dependencies>
</project>

Please correct me what I am doing wrong, I am Expecting it to run properly but I am getting an error message:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ab.repository.WebServiceRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Solution

  • After looking through your code that you posted on github i pulled it and rightfully so you have problems with your dependencies. You where missing the spring build plugin that includes all the dependencies in the jar.

    Always use the Spring initlizr when starting a new project and it will set up all this for you automatically (unless you have good experience with spring and know what you are doing).

    fully working pom.xml

    <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.ab</groupId>
        <artifactId>SpringBootTry</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        // latest version of spring as of writing 2.1.7
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.7.RELEASE</version>
        </parent>
    
        // set what version you want of java 1.8, 9, 10, 11, 12?
        <properties>
            <java.version>11</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
        </dependencies>
    
        // You need the spring plugin to bild a fat jar that includes all
        // the dependencies. Without this, no dependencies are included in 
        // the jar and you get NoSuchBeanDefinitionexception
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    you also need to update your application properties

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    

    or else you will get a warning when you start your server.