简体   繁体   中英

Spring Boot (Kotlin) Autowire failure for generic bean with List<X>

I have this bean defined in a Batch application:

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

I know that the above bean gets created in the app, I put a breakpoint in the init method and it stops when I debug the app.

I have this class:

@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.

I get an error saying that the "updating-processor" cannot be autowired, and it's due to the List<OtherObject> as the second type. If I change List<OtherObject> to just OtherObject for the second generic parameter, the autowiring works.

How do I make it work with the list?

This is related to this post -> spring-inject-list-of-generic-interface-implementations-in-kotlin

You should be able to take the Action suggested by Spring:

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

For example:

@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")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM