SpringBootKotlin找不到存储库bean

pu82cl6c  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(273)

我刚刚开始使用SpringBoot+kotlin,我正在尝试 PagingAndSortingRepository jpa的接口,因此我编写了以下接口:

interface CustomerRepository : PagingAndSortingRepository<Customer, Long>

模型 Customer 如下所示:

@Entity
data class Customer(
    @Id @GeneratedValue var id: Long,
    var name: String
)

现在我想用一个 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)
}

以及 Application 看起来像这样:

@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生成一个: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.

我看到了一些关于这个的不同帖子,并尝试添加 Configuration , AutoConfiguration 以及 EnableJpaRepositories 但是注解只将错误更改为 entityManagerFactory 未找到而不是 CustomerRepository 找不到。

qlzsbp2j

qlzsbp2j1#

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

spring.jpa.hibernate.ddl-auto=update

在application.properties中。您还缺少@autowired注解。 entityManagerFactory 缺少,因为默认的auo配置已关闭,在这种情况下,应用程序希望您执行所有必要的配置,而您又没有执行这些配置。因此,请保持默认配置,并更改所需内容。
此代码假定在单个文件中。
如果您有多个软件包,则可能需要添加此链接中提到的工作代码:

package com.example.demo

import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import lombok.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
import org.springframework.web.bind.annotation.*
import java.util.*
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.persistence.Table

@SpringBootApplication
open class SpringBootDerbyAppApplication

fun main(args: Array<String>) {
    runApplication<SpringBootDerbyAppApplication>(*args)
}

@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "applog")
internal class AppLog {
    @Id
    @GeneratedValue
    private val id: Long = 0

    @JsonProperty
    private val name: String? = null
}

@Configuration
open class ObjectMapperConfiguration {
    @Bean
    @Primary
    open fun objectMapper() = ObjectMapper().apply {
        registerModule(KotlinModule())
    }
}

@RestController
@RequestMapping(path = ["/logs"])
internal class LogController @Autowired constructor(private val appLogRepository: AppLogRepository) {
    @GetMapping(path = ["/"])
    fun logs(): MutableIterable<AppLog> {
        return appLogRepository.findAll()
    }

    @PostMapping(path = ["/"])
    fun add(@RequestBody appLog: AppLog): AppLog {
        appLogRepository.save(appLog)
        return appLog
    }

}

@Repository
internal interface AppLogRepository : CrudRepository<AppLog, Long>

渐变文件

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

相关问题