即使在 build.gradle 中指定,也无法解析符号“EnableJpaRespositories”

Posted

技术标签:

【中文标题】即使在 build.gradle 中指定,也无法解析符号“EnableJpaRespositories”【英文标题】:Cannot resolve symbol 'EnableJpaRespositories' even though specified in build.gradle 【发布时间】:2021-06-30 16:34:58 【问题描述】:

由于某种原因,我无法在我的项目中获取一个模块来检测它是否具有获取 @EnableJpaRepositories 的依赖项。尽管在我的 build.gradle 文件中指定了 implementation 'org.springframework.boot:spring-boot-starter-data-jpa',但当我运行 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'

【问题讨论】:

【参考方案1】:

您在@EnableJpaRespositories 中有错字。

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

【讨论】:

以上是关于即使在 build.gradle 中指定,也无法解析符号“EnableJpaRespositories”的主要内容,如果未能解决你的问题,请参考以下文章

gradle 不会从我在 build.gradle 中指定的库中的依赖版本中的父属性中解析占位符

即使在运行配置中指定,Eclipse 也无法找到 adb

即使我在生产者配置中指定了压缩类型,kafka 代理也没有压缩我更大的消息

为啥即使源代码没有变化,Gradle 的“构建”任务也不是最新的?

Flutter 构建失败('无法读取包名')

Gradle修改Maven仓库地址