java - 我在使用 JpaRepository 创建简单的 Spring Boot 项目时遇到 NoSuchBeanDefinitionException

标签 java spring-boot jpa spring-boot-jpa

ScreenShot of My Project Structure from STS

我正在使用 JpaRepository 创建一个简单的 Spring Boot 应用程序,但是当我尝试运行我的应用程序时,它给出了一个错误“NoSuchBeanDefinitionException”。我是 Spring Boot 新手。

我还尝试用 @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.

以前我没有使用这个注释,但我在一个问题中看到我必须告诉我的类启用 JPA 存储库,所以我也尝试了这个,但它也不起作用。

我的主课


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 类是:


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

服务等级:


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

存储库接口(interface):


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

import com.ab.model.WebServiceModel;

public interface WebServiceRepository extends JpaRepository<WebServiceModel, Integer> {

}

我的 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>

请纠正我做错了什么,我希望它能够正常运行,但我收到一条错误消息:

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

最佳答案

查看了您在 github 上发布的代码后,我将其拉出,因此您的依赖项出现了问题。您缺少包含 jar 中所有依赖项的 spring 构建插件。

始终使用Spring initlizr当开始一个新项目时,它会自动为您设置所有这些(除非您对 Spring 有良好的经验并且知道自己在做什么)。

完全工作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>

您还需要更新您的应用程序属性

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

否则在启动服务器时您会收到警告。

关于java - 我在使用 JpaRepository 创建简单的 Spring Boot 项目时遇到 NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57740335/

相关文章:

jakarta-ee - TypedQuery 代替 JPA 中的普通查询

java - 如何在 hibernate 中将枚举序号映射到它的值?

java - 如何实现 ActionListener 以使退出按钮起作用

JavaFX GridPane 列/行跨度与索引

java - WildFly/JBoss服务器组部署理解

java - 我不能在我的代码中使用 findOne() 方法

java - 构造函数不能应用于给定类型?实际参数列表和形式参数列表的长度不同

java - 创建 DatagramSocket 之前检查端口的可用性

mysql - 您的 SQL 语法有错误; ... 'UNIQUE ` UK6i9q4suadww4j167aqe2h6aqj`' 在第 4 行

JPA 2.0 : Batch query, 安全且高性能?