0

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)}
halfer
  • 19,824
  • 17
  • 99
  • 186
Amit
  • 231
  • 1
  • 8
  • 19
  • Amit, what is the package name in which WebServiceRepository resides, and what is the package name where main class resides? – Faraz Aug 31 '19 at 17:50
  • Faraz sir, package name for WebServiceRepository is com.ab.repository, and for main class, it is, com.ab – Amit Aug 31 '19 at 17:56
  • remove `@repository` annotation – Faraz Aug 31 '19 at 17:59
  • If I remove it, then how can spring detect that this class is a repository class, and even if I remove it then again it will throw the same exception that there is no bean for WebServiceRepository. – Amit Aug 31 '19 at 18:03
  • That's because your repository class extends JpaRepository, which in turn extends Spring's Repository class. So when spring encounters a subclass of Spring's Repository class, it automatically detects it and creates beans of it. – Faraz Aug 31 '19 at 18:07
  • But i am facing same error again if i remove it. Because i am autowiring it in my WebSrvService class. – Amit Aug 31 '19 at 18:11
  • go to this article https://spring.io/guides/gs/accessing-data-jpa/ and CTRL + F **to create repository implementations automatically** – Faraz Aug 31 '19 at 18:13
  • any reason to why you are running a realy old version of Spring version 1.5.6.RELEASE and you dont need spring orm, hibernate, or the mysql connector. All you need is to pull in the spring jpa starter and you will get all that. – Toerktumlare Aug 31 '19 at 21:39
  • @Amit Have you solved your issue ? – Avijit Barua Sep 01 '19 at 08:04
  • 1
    @Amit can you please share your project structure? – Dildeep Singh Sep 01 '19 at 09:22
  • second thing remove version from all of your dependency. will manage version for you. – Dildeep Singh Sep 01 '19 at 09:30
  • @DildeepSingh sir, i have removed versions from other dependencies as you said. But NoSuchBeanException is still not resolved. – Amit Sep 01 '19 at 11:38
  • can you please provide the screenshot of your project structure means where you place your controller services and repository – Dildeep Singh Sep 01 '19 at 11:45
  • @DildeepSingh, I have edited my question with the screenshot, Could you please guide me now. – Amit Sep 01 '19 at 11:55

3 Answers3

3

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.

Toerktumlare
  • 12,548
  • 3
  • 35
  • 54
  • I have added 2.1.7 version in my pom and i have got an error in my pom.xml file "Description Resource Path Location Type Unknown pom.xml /SpringBootTry line 1 Maven Configuration Problem ". I have seen some examples over the internet and they were using the same old verison and it was working fine for them. – Amit Sep 01 '19 at 05:22
  • I have added version 2.0.2 and this time the error which I was facing with 2.1.7 has gone, But still, I am getting the same problem that, "NoSuchBeanDefinitionException". – Amit Sep 01 '19 at 05:36
  • the error seems to be a bug in eclipse https://stackoverflow.com/questions/56212981/eclipse-showing-maven-configuration-problem-unknown – Toerktumlare Sep 01 '19 at 10:47
  • you should really not use an old version of spring boot that has no support. – Toerktumlare Sep 01 '19 at 10:47
  • I think 2.0.2 version is not too old to use. And i am not getting any error with this version in pom.xml file. But still that NoSuchBeanException is still not resolved. – Amit Sep 01 '19 at 11:35
  • you still havn't posted your project structure. And you have clearly more problems in your project in code that you are not posting. So please put up your code on github so we can all look through the projects entirety – Toerktumlare Sep 01 '19 at 11:43
  • I have edited my question with a screenshot of the project structure, and the GitHub project link is: [link](https://github.com/Abvani-0204/Spring-Boot-POC) – Amit Sep 01 '19 at 13:49
0

Remove “@Repository”, maybe you can

马向峰
  • 13
  • 3
  • how can spring detect that this class is a repository class, and even if I remove it then again it will throw the same exception that there is no bean for WebServiceRepository, because I am autowiring this class in another class. – Amit Aug 31 '19 at 18:04
  • Check the configuration file : application.yaml. – 马向峰 Aug 31 '19 at 18:17
-1

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.