Amit
Amit

Reputation: 271

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)}

Upvotes: 0

Views: 984

Answers (3)

Toerktumlare
Toerktumlare

Reputation: 14732

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.

Upvotes: 3

Dildeep Singh
Dildeep Singh

Reputation: 108

enter image description here

I created both the folder structure and code like yours.

package com.ab;

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

@SpringBootApplication
public class SpringBootTry1Application {

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

}

Why you change your main class name?Spring boot automatically create main class for you.


package com.ab.controller;

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

import com.ab.model.WebServiceModel;
import com.ab.service.WebService;
@Controller
public class WebServiceController {
    @Autowired
    private WebService webSrvService;

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


package com.ab.service;

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 WebService {
    @Autowired
    private WebServiceRepository webServiceRepository;

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

    }
}


package com.ab.repository;

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

import com.ab.model.WebServiceModel;

public interface WebServiceRepository extends JpaRepository<WebServiceModel,Integer>{

}

## LOGS ##

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.7.RELEASE)

2019-09-01 18:26:27.412  INFO 5522 --- [           main] com.ab.SpringBootTry1Application         : Starting SpringBootTry1Application on BGINMAC004.local with PID 5522 (/Users/Dildeep.Singh/Documents/workspace-sts-3.9.9.RELEASE/SpringBootTry-1/target/classes started by Dildeep.Singh in /Users/Dildeep.Singh/Documents/workspace-sts-3.9.9.RELEASE/SpringBootTry-1)
2019-09-01 18:26:27.414  INFO 5522 --- [           main] com.ab.SpringBootTry1Application         : No active profile set, falling back to default profiles: default
2019-09-01 18:26:27.992  INFO 5522 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-09-01 18:26:28.055  INFO 5522 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 57ms. Found 1 repository interfaces.
2019-09-01 18:26:28.354  INFO 5522 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$8d03f4e2] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-09-01 18:26:28.583  INFO 5522 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-09-01 18:26:28.602  INFO 5522 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-09-01 18:26:28.603  INFO 5522 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-09-01 18:26:28.692  INFO 5522 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-09-01 18:26:28.692  INFO 5522 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1250 ms
2019-09-01 18:26:28.839  INFO 5522 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-09-01 18:26:28.937  INFO 5522 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-09-01 18:26:28.981  INFO 5522 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2019-09-01 18:26:29.141  INFO 5522 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.10.Final}
2019-09-01 18:26:29.142  INFO 5522 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-09-01 18:26:29.228  INFO 5522 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-09-01 18:26:29.316  INFO 5522 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2019-09-01 18:26:29.743  INFO 5522 --- [           main] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@3850e90c'
2019-09-01 18:26:29.746  INFO 5522 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-09-01 18:26:30.149  INFO 5522 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-01 18:26:30.210  WARN 5522 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-09-01 18:26:30.417  INFO 5522 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-01 18:26:30.420  INFO 5522 --- [           main] com.ab.SpringBootTry1Application         : Started SpringBootTry1Application in 18.317 seconds (JVM running for 23.971)
2019-09-01 18:41:39.601  INFO 5522 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-09-01 18:41:39.602  INFO 5522 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-09-01 18:41:39.615  INFO 5522 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 13 ms
2019-09-01 18:47:48.219  WARN 5522 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=6m18s959ms).

My code compiles fine. Try to create new spring starter project from STS and do not touch spring boot main class and copy the same folder structure from here.Your code will definitely compiles.

Upvotes: -2

马向峰
马向峰

Reputation: 11

Remove “@Repository”, maybe you can

Upvotes: -1

Related Questions