Spring Boot Kotlin找不到存储库bean

Spring Boot Kotlin找不到存储库bean,spring,spring-boot,kotlin,spring-data-jpa,Spring,Spring Boot,Kotlin,Spring Data Jpa,我刚刚开始使用Spring Boot+Kotlin,我尝试了JPA的PagingAndSortingRepository接口,因此我编写了以下接口: interface CustomerRepository : PagingAndSortingRepository<Customer, Long> 现在,我正尝试将其与CustomerService连接起来,该服务如下所示: @Service class CustomerService( private val custome

我刚刚开始使用Spring Boot+Kotlin,我尝试了JPA的
PagingAndSortingRepository
接口,因此我编写了以下接口:

interface CustomerRepository : PagingAndSortingRepository<Customer, Long>
现在,我正尝试将其与
CustomerService
连接起来,该服务如下所示:

@Service
class CustomerService(
    private val customerRepository: CustomerRepository
) {
    fun getAllCustomers(): Collection<Customer> = customerRepository.findAll().toList()
    fun addCustomer(customer: Customer) = customerRepository.save(customer)
    fun deleteCustomer(customer: Customer) = customerRepository.delete(customer)
    fun updateCustomer(customer: Customer) = customerRepository.save(customer)
}
@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories
class Application

fun main(args: Array<String>) {
    runApplication<Application>(*args)
}
我已经添加了我认为必需的依赖项,如下所示:

plugins {
    id("org.springframework.boot") version "2.5.0-SNAPSHOT"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.4.30"
    kotlin("plugin.spring") version "1.4.30"
}

implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.apache.derby:derby:10.15.2.0")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
SpringBoot无法找到某种意义上的bean,因为我还没有定义一个。然而,阅读文档时,似乎应该在此处由Spring Boot生成一个:

Application.properties是

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

我收到的错误消息是:

Description:
Parameter 0 of constructor in com.ubiquifydigital.crm.service.CustomerService required a bean named 'entityManagerFactory' that could not be found.

Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.

我看到了一些关于此的不同帖子,并尝试添加
配置
AutoConfiguration
EnableJpaRepositories
注释,但这只会将错误更改为
entityManagerFactory
未找到,而不是
CustomerRepository
未找到。

在内存数据库中使用默认值时,必须定义

spring.jpa.hibernate.ddl-auto=update
在application.properties中。您还缺少@Autowired注释
entityManagerFactory
丢失,因为默认的auo配置已关闭,在这种情况下,应用程序希望您执行所有必要的配置,而您没有执行这些配置。因此,保持默认配置打开,并更改所需内容

此代码假定位于单个文件中

如果您有多个软件包,那么您可能需要添加本文中提到的内容 工作代码:

package com.example.demo
导入com.fasterxml.jackson.annotation.JsonProperty
导入com.fasterxml.jackson.databind.ObjectMapper
导入com.fasterxml.jackson.module.kotlin.KotlinModule
进口龙目山*
导入org.springframework.beans.factory.annotation.Autowired
导入org.springframework.boot.autoconfigure.springboot应用程序
导入org.springframework.boot.runApplication
导入org.springframework.context.annotation.Bean
导入org.springframework.context.annotation.Configuration
导入org.springframework.context.annotation.Primary
导入org.springframework.data.repository.crudepository
导入org.springframework.stereotype.Repository
导入org.springframework.web.bind.annotation*
导入java.util*
导入javax.persistence.Entity
导入javax.persistence.GeneratedValue
导入javax.persistence.Id
导入javax.persistence.Table
@SpringBoot应用程序
开放类SpringBootDerbyapplication
趣味主线(args:Array){
运行应用程序(*args)
}
@实体
@吸气剂
@塞特
@AllArgsConstructor
@诺尔格构装师
@表(name=“applog”)
内部类AppLog{
@身份证
@生成值
私有值id:Long=0
@JsonProperty
私有值名称:字符串?=null
}
@配置
开放类ObjectMapperConfiguration{
@豆子
@初级的
打开objectMapper()=objectMapper()。应用{
registerModule(KotlinModule())
}
}
@RestController
@请求映射(路径=[“/logs”])
内部类LogController@Autowired构造函数(私有val appLogRepository:appLogRepository){
@GetMapping(路径=[“/”])
有趣的日志():可变的{
返回appLogRepository.findAll()
}
@后期映射(路径=[“/”])
趣味添加(@RequestBody appLog:appLog):appLog{
appLogRepository.save(appLog)
返回应用程序
}
}
@存储库
内部接口AppLogRepository:Crudepository
梯度锉

plugins {
    id 'org.springframework.boot' version '2.4.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.5.0-M1'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
    maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'org.apache.derby:derby'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")

}

test {
    useJUnitPlatform()
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}


你能不能跟我说说关于建立德比的问题。我不觉得这有什么问题。我已经删除了我提到的derby部分,这是一个旁注,不是我面临的问题。请显示application.properties我发布的问题包含application.properties和gradle.kts中的依赖项。我没有使用Maven为什么要禁用
DataSourceAutoConfiguration
autoconfiguration?谢谢,我会尝试一下并让您知道@Kotlin不需要使用Autowired进行新的Spring引导更改。我已经能够创建模拟类,这些类在没有自动连线注释的情况下工作得非常好。看起来只是使用运行时derby并更新应用程序配置修复了它。非常感谢。
plugins {
    id 'org.springframework.boot' version '2.4.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.5.0-M1'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
    maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'org.apache.derby:derby'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")

}

test {
    useJUnitPlatform()
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}