简体   繁体   English

我在使用 JpaRepository 创建一个简单的 Spring Boot 项目时得到 NoSuchBeanDefinitionexception

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

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".我正在使用 JpaRepository 创建一个简单的 Spring Boot 应用程序,但是当我尝试运行我的应用程序时,它给出了一个错误“NoSuchBeanDefinitionException”。 I am new to Spring Boot.我是 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我还尝试使用 @SpringBootApplication @EnableJpaRepositories("com.ab.repository")注释来注释我的主类,但是每当我尝试注释@EnableJpaRepository() ,它都会在 STS 中显示错误

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.以前我没有使用这个注释,但我在一个问题中看到我必须告诉我的班级启用 JPA 存储库,所以我也尝试了这个,但它也不起作用。

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:我的 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>
    <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)}

After looking through your code that you posted on github i pulled it and rightfully so you have problems with your dependencies.在查看了您在 github 上发布的代码后,我正确地将其拉出,因此您的依赖项存在问题。 You where missing the spring build plugin that includes all the dependencies in the jar.您缺少包含 jar 中所有依赖项的 spring 构建插件。

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).在开始一个新项目时总是使用Spring initlizr ,它会自动为你设置所有这些(除非你对 spring 有很好的经验并且知道你在做什么)。

fully working pom.xml完全工作的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.否则,您将在启动服务器时收到警告。

删除“@Repository”,也许你可以

在此处输入图片说明

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.尝试从 STS 创建新的 spring starter 项目,不要触摸 spring boot 主类并从这里复制相同的文件夹结构。您的代码肯定会编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM