Spring Boot 2.0.0.M7,Kotlin,Gradle-bean无法find

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


应用程序启动失败


描述:

com.mycompany.app.rest.api.MyApi中构造函数的参数1需要一个无法find的“com.mycompany.app.domain.UserRepository”types的bean。

行动:

考虑在你的配置中定义一个types为“com.mycompany.app.domain.UserRepository”的bean。

MYAPP:

MyAPP目录结构和文件

Myapp.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) { runApplication(*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 { 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) } } 

User.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 { } 

的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中的上述目录,但没有成功。

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

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