spring-boot - 使用kotlin在SpringBoot中映射结构接口错误
问题描述
我将 java spring boot 代码转换为 kotlin,现在我在 map struct 接口和 bean config 类中有错误
这是我的映射器界面
@Component
@Mapper
interface PersonMapper {
fun toPerson(personDTO: PersonDTO?): Person?
fun toPersonDTO(person: Person?): PersonDTO?
fun toPersons(personDTOS: List<PersonDTO?>?): List<Person?>?
fun toPersonDTOs(personList: List<Person?>?): List<PersonDTO?>?
companion object {
@JvmStatic
val INSTANCE: PersonMapper = Mappers.getMapper(PersonMapper::class.java)
}
}
和我的 Bean 配置
@Configuration
class BeanConfig {
@Bean
@Primary
fun personMapper(): PersonMapper {
return PersonMapper.INSTANCE
}
}
和我的错误
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personController' defined in file [D:\Zaban\Server\target\classes\ir\ktcoders\zaban\person\PersonController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personService' defined in file [D:\Zaban\Server\target\classes\ir\ktcoders\zaban\person\PersonService.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personMapper' defined in class path resource [ir/ktcoders/zaban/config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [ir.ktcoders.zaban.person.PersonMapper]: Factory method 'personMapper' threw exception; nested exception is java.lang.ExceptionInInitializerError
解决方案
首先,您应该@Component
从界面中删除 ,因为它没有带来任何价值(但它也不是错误的罪魁祸首)。
很难说更多,但ExceptionInInitializerError
看起来Mappers
课堂上有问题,所以请提供它的代码。也许它还没有初始化并且找不到这个映射器?
推荐阅读
- matlab - matlab不能赋值数组
- c# - Application Lifecycle for Xamarin.iOS
- javascript - 将方法本身作为方法调用
- c# - 创建字节数组而不用 0 初始化
- php - 使用 Laravel 5.7 在邮递员中失败的 POST 请求
- sql - 是否有任何功能可以通过某些结果插入多于一行的触发器组?
- android - 将一个类传递给 Kotlin 函数以在适配器中创建 ViewHolder
- php - 如何从文本文件中读取列并插入数据库php
- ajax - 具有 multipart/form-data enctype 的表单选择元素
- c# - 如何在松弛的附件消息中添加提及?