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

时间:2018-01-04 22:39:20

标签: spring-boot kotlin

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

        

申请失败

           

说明

     

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

     

动作:

     

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

MyApp的:

MyAPP directory structure and files

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<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)

    }
}



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

1 个答案:

答案 0 :(得分:0)

您必须将@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