尝试在Kotlin和Spring启动时使用JPA时BeanDefinitionParsingException

时间:2017-05-29 14:59:27

标签: spring maven jpa spring-boot kotlin

当我尝试使用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')"
    }
}

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

1 个答案:

答案 0 :(得分:0)

从日志中看起来它实际上是第4课session_start(); $sdemail=$_SESSION['sdemail']; // refer 1 $_SESSION['sdemail'] = $sdemail; // just replace the above line with this In dashboard.php if(isset($_SESSION['sdemail'])) { echo $sdemail; }// replace this ,您遇到了问题。它说该类中的Application方法不应该是initprivate,这里的问题是后者 - Kotlin中的函数和类是final默认情况下。

您可以使用final关键字标记该方法,使其成为非最终方法。

open

您还必须使用@Bean open fun init(repository: CustomerRepository) = CommandLineRunner { ... } 关键字标记Application课程。

open

这些@SpringBootApplication open class Application { ... } 关键字不在示例中的原因是因为它使用了kotlin-spring插件(see in the build script),这打开了Spring需要的类和函数最终

如果您不想手动制作课程open,您也应该可以将kotlin-spring插件与Maven一起使用。这样做的说明就在Gradle指令的正上方,在关于all-open插件的章节末尾。您只需要包含其中的代码,并将open替换为<plugin>all-open</plugin>,如其中所述的评论所述。