【问题标题】:BeanDefinitionParsingException when trying to use JPA with Kotlin and Spring boot尝试将 JPA 与 Kotlin 和 Spring 引导一起使用时出现 BeanDefinitionParsingException
【发布时间】:2017-10-29 22:32:05
【问题描述】:

当我尝试使用 springboot + JPA + kotlin + maven 时出现此异常

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: 
Configuration problem: @Bean method 'init' must not be private or final; 
change the method's modifiers to continue
Offending resource: com.wirecard.kotlin.jpa.Application
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
at org.springframework.context.annotation.BeanMethod.validate(BeanMethod.java:50)
at org.springframework.context.annotation.ConfigurationClass.validate(ConfigurationClass.java:219)
at org.springframework.context.annotation.ConfigurationClassParser.validate(ConfigurationClassParser.java:528)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:307)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:239)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.wirecard.kotlin.jpa.ApplicationKt.main(Application.kt:50)

我按照此链接中的示例进行操作,但它是使用 Gradle 构建的

Springboot application for kotlin and JPA

代码由3个类组成

类客户

import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id

@Entity
class Customer(
    val firstName: String,
    val lastName: String,
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    val id: Long = -1) {

    override fun toString(): String {
    return "Customer(id=$id, firstName='$firstName', lastName='$lastName')"
    }
}

类 CustomerController

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController

@RestController
class CustomerController(val repository:CustomerRepository) {

    @GetMapping("/")
    fun findAll() = repository.findAll()

    @GetMapping("/{lastName}")
    fun findByLastName(@PathVariable lastName:String)
            = repository.findByLastName(lastName)
}

接口 CustomerRepository

​​>
import org.springframework.data.repository.CrudRepository

interface CustomerRepository : CrudRepository<Customer, Long> {

    fun findByLastName(lastName: String): Iterable<Customer>
}

【问题讨论】:

    标签: spring maven jpa spring-boot kotlin


    【解决方案1】:

    从日志看来,您遇到的问题实际上是第 4 类 Application。它说该类中的init 方法不应该是privatefinal,你的问题是后者——Kotlin 中的函数和类默认是final

    您可以通过使用 open 关键字标记方法来使该方法成为非最终方法。

    @Bean
    open fun init(repository: CustomerRepository) = CommandLineRunner { ... }
    

    您还必须使用 open 关键字标记您的 Application 类。

    @SpringBootApplication
    open class Application { ... }
    

    示例中没有这些open 关键字的原因是因为它使用了kotlin-spring 插件(see in the build script),它打开了Spring 要求非final 的类和函数。

    如果您不想手动创建类open,您也应该能够将kotlin-spring 插件与Maven 一起使用。这样做的说明就在 Gradle 说明的正上方,在关于 all-open 插件的章节的末尾。您只需要包含那里的代码,并将&lt;plugin&gt;all-open&lt;/plugin&gt; 替换为&lt;plugin&gt;spring&lt;/plugin&gt;,正如那里的评论所述。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 2017-02-21
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 2014-05-17
      相关资源
      最近更新 更多