gpt4 book ai didi

spring - 带有List 的通用bean的Spring Boot(Kotlin)自动接线失败

转载 作者:行者123 更新时间:2023-12-02 13:34:39 26 4
gpt4 key购买 nike

我在批处理应用程序中定义了这个bean:

@Service("updating-processor")
class UpdatingProcessor(val searchService: searchService, val objectMapper: ObjectMapper) : ItemProcessor<SomeItem, List<OtherObject>>

我知道上面的bean是在应用程序中创建的,我在init方法中设置了一个断点,并且在调试应用程序时它会停止。

我有这个课:
@SpringBootApplication
@EnableBatchProcessing
@EnableJpaRepositories
@EnableRetry(proxyTargetClass=true)
class EtlApplication() : CommandLineRunner {
companion object {
val LOG = LoggerFactory.getLogger(EtlApplication::class.java.name)
}

@Autowired
@Qualifier("updating-processor")
lateinit var updatingProcessor: ItemProcessor<SomeItem, List<OtherObject>>
}


APPLICATION FAILED TO START


Description:

Field updatingProcessor in ... required a bean of type >> 'org.springframework.batch.item.ItemProcessor' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.batch.item.ItemProcessor' in your configuration.



我收到一个错误消息,说“更新处理器”无法自动接线,这是由于 List<OtherObject>作为第二种类型。如果将第二个通用参数的 List<OtherObject>更改为 OtherObject,则 Autowiring 有效。

如何使它与列表一起使用?

最佳答案

这与此帖子有关-> spring-inject-list-of-generic-interface-implementations-in-kotlin

您应该可以采用Spring建议的操作:

Consider defining a bean of type 'org.springframework.batch.item.ItemProcessor' in your configuration.



例如:
@Configuration
class UpdatingProcessorConfig {
@Autowired
lateinit var searchService: SearchService
@Autowired
lateinit var objectMapper: ObjectMapper

@Bean
fun updatingProcessor(): ItemProcessor<SomeItem, List<OtherObject>> = UpdatingProcessor(searchService, objectMapper)
}

class UpdatingProcessor(val searchService: SearchService, val objectMapper: ObjectMapper) :
ItemProcessor<SomeItem, List<OtherObject>> {
override fun process(p0: SomeItem): List<OtherObject>? {
TODO("not implemented")
}
}

关于spring - 带有List <X>的通用bean的Spring Boot(Kotlin)自动接线失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59535916/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com