【问题标题】:Spring Boot 2.0.0.M7, Kotlin, Gradle - bean couldn't be foundSpring Boot 2.0.0.M7,Kotlin,Gradle - 找不到bean
【发布时间】:2018-01-04 22:39:20
【问题描述】:

我正在尝试运行一个使用 Kotlin 编写并使用 Gradle 构建的小型 Spring Boot 应用程序。当我在 IntelliJ 中运行应用程序时,我收到以下消息:


应用程序启动失败


说明:

com.mycompany.app.rest.api.MyApi 中构造函数的参数1 需要一个 'com.mycompany.app.domain.UserRepository' 类型的 bean 找不到。

行动:

考虑定义一个 bean 类型 'com.mycompany.app.domain.UserRepository' 在您的配置中。

我的APP:

MyAPP directory structure and files

我的应用程序.kt

package com.mycompany.app.startbootapp

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.ComponentScan


@SpringBootApplication
@ComponentScan(basePackages = arrayOf("com.mycompany.app"))
class MyApp

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



MyApi.kt

package com.mycompany.app.rest.api

import com.mycompany.app.domain.User
import com.mycompany.app.domain.UserRepository
import com.mycompany.app.infra.ParamUtil
import com.mycompany.app.rest.api.inout.DtoUser
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@RestController
class MyApi(val paramUtil: ParamUtil, val repo: UserRepository) {


    @PostMapping("/user")
    fun tokenJwtUsuario(@RequestBody dto: DtoUser, request: HttpServletRequest, response: HttpServletResponse):
            ResponseEntity<String> {
        if (!paramUtil.verificaParam(dto.id) && !paramUtil.verificaParam(dto.name)) {
            return ResponseEntity("Invalid parameter provided !", HttpStatus.BAD_REQUEST)
        }
        var user = User(dto.id, dto.name)
        repo.save(user)
        return ResponseEntity(HttpStatus.OK)

    }
}



用户.kt

package com.mycompany.app.domain

import javax.persistence.Entity
import javax.persistence.Id

@Entity
data class User(

        @Id
        val userID: String,
        val name: String
) { }



UserRepository.kt

package com.mycompany.app.domain

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

@Repository
interface UserRepository: JpaRepository<User, String> {
}


build.gradle

buildscript {
    ext {
        kotlinVersion = '1.2.10'
        springBootVersion = '2.0.0.M7'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
    }
}


apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'

group = 'com.mycompany'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

repositories {
    mavenCentral()
    mavenLocal()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}


configurations {
    compile.exclude module: "spring-boot-starter-tomcat"
}


dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("com.fasterxml.jackson.module:jackson-module-kotlin")
    compile("org.springframework.boot:spring-boot-starter-undertow")
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-hateoas')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('org.springframework.kafka:spring-kafka')
    compile('org.springframework.boot:spring-boot-starter-webflux')
    compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:1.2.10")
    compile("org.jetbrains.kotlin:kotlin-reflect")
    compile("com.h2database:h2")
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('io.projectreactor:reactor-test')
}



我尝试将 MyApp.kt 移动到 com.mycompany.app 中的上述目录,但没有成功。

【问题讨论】:

  • 将主类移动到包com.mycompany.app,它应该开始工作了。
  • 正如我在原帖中提到的,我试过这个,但没有成功。

标签: spring-boot kotlin


【解决方案1】:

您必须将@EnableJpaRepositories 和@EntityScan 添加到您的MyApp-Class

@SpringBootApplication
@ComponentScan(basePackages = arrayOf("com.mycompany.app"))
@EnableJpaRepositories(basePackages = arrayOf("com.mycompany.app.domain"))
@EntityScan(value = "com.mycompany.app.domain")
class MyApp

【讨论】:

  • 谢谢雷内。这个对我有用。我一直在寻找 Spring Boot 文档,但不清楚此设置 @EnableJpaRepositories 的义务。 Session 77.3 在文档中使用 Spring Data 存储库。
  • @etc 如果这个答案对你有帮助,请接受这个答案,这对其他 SOers 也有帮助。
猜你喜欢
  • 2018-10-01
  • 2021-06-09
  • 2018-06-17
  • 2018-05-17
  • 2018-03-16
  • 2022-11-10
  • 2018-06-29
  • 2018-12-13
  • 1970-01-01
相关资源
最近更新 更多