首页 > 解决方案 > 即使在 build.gradle 中指定,也无法解析符号“EnableJpaRespositories”

问题描述

出于某种原因,我无法在我的项目中获取一个模块来检测它是否具有合适的依赖项来获取@EnableJpaRepositories. 尽管implementation 'org.springframework.boot:spring-boot-starter-data-jpa'在我的 build.gradle 文件中指定了,当我运行 gradle 来尝试编译时,我收到以下错误:

./gradlew clean build 

> Task :rest:compileJava FAILED
/Users/pasdeignan/git/pasciifinance/rest/src/main/java/com/pasciitools/pasciifinance/rest/PasciifinanceApplication.java:22: error: cannot find symbol
@EnableJpaRespositories(basePackages="com.pasciitools.pasciifinance")
 ^
  symbol: class EnableJpaRespositories
1 error

FAILURE: Build failed with an exception.

背景

我有一个正在构建的多模块 Spring 项目,我试图将 JPA 片段存储在一个common模块中,并将 REST 服务存储在一个rest模块中。该rest模块也是我SpringBootApplication生活的地方。当我启动应用程序时,我收到以下错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field accountRepo in com.pasciitools.pasciifinance.rest.PasciifinanceApplication required a bean of type 'com.pasciitools.pasciifinance.common.repositories.AccountRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.pasciitools.pasciifinance.common.repositories.AccountRepository' in your configuration.

项目结构

pasciifinance
└── build.gradle (empty)
└── settings.gradle
└── common
     └── build.gradle
     └── src
          └── main/java/com/pasciitools/pasciifinance/common
                └── entity
                └── repository
                    └── AccountRepository.java
└── rest
     └── build.gradle
     └── src
         └── main/java/com/pasciitools/pasciifinance/rest
             └── PasciiFinanceApplication.java
             └── restservice
                 └── RestService.java

PasciiFinanceApplication.java

package com.pasciitools.pasciifinance.rest;

import com.pasciitools.pasciifinance.common.entity.Account;
import com.pasciitools.pasciifinance.common.entity.AccountEntry;
import com.pasciitools.pasciifinance.common.repository.AccountEntryRepository;
import com.pasciitools.pasciifinance.common.repository.AccountRepository;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.List;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@EnableEncryptableProperties
@EnableJpaRespositories(basePackages="com.pasciitools.pasciifinance")
@SpringBootApplication(scanBasePackages = "com.pasciitools.pasciifinance")
public class PasciifinanceApplication {


    @Autowired
    private AccountRepository accountRepo;

    @Autowired
    private AccountEntryRepository entryRepo;

    private static final Logger log = LoggerFactory.getLogger(PasciifinanceApplication.class);

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

build.gradle(休息模块)


plugins {
    id 'org.springframework.boot' version '2.4.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.pascii-tools'
version = '0.0.1-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation project(':common')
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    implementation 'org.apache.poi:poi:5.0.0'
    implementation 'org.apache.poi:poi-ooxml:5.0.0'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.seleniumhq.selenium:selenium-java:3.141.59'
    implementation 'com.github.ulisesbocchio:jasypt-spring-boot:3.0.3'
    implementation 'org.springframework.boot:spring-boot-starter-batch'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    runtimeOnly 'com.h2database:h2'
}

test {
    useJUnitPlatform()
}

build.gradle(通用)

plugins {
    id 'org.springframework.boot' version '2.4.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.pascii-tools'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 11

repositories {
    mavenCentral()
}

bootJar {
    enabled = false
}

jar {
    enabled = true
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

标签: javaspring-boothibernategradlespring-data-jpa

解决方案


你有一个错字@EnableJpaRespositories

@EnableJpaRepositories正如您在问题中提出的那样,您需要将其重命名为 。


推荐阅读