Search code examples
kotlinspring-batchfeign

Spring Batch 5 - Autowiring Feign Client bean not found


I have a Spring Batch application which has a processor which fetches metadata from an API with Openfeign. The FetchMetaDataProcessor autowires the MoviesRemote class but states "Could not autowire. No beans of 'MoviesRemote' type found.".

@SpringBootApplication
@EnableFeignClients(basePackages = ["be.robbevw.fliximportmoviesjob.remotes"])
class FlixImportMoviesJobApplication

fun main(args: Array<String>) {
    runApplication<FlixImportMoviesJobApplication>(*args)
}

@FeignClient(name = "movies", url = "https://api.themoviedb.org/3")
interface MoviesRemote {

    @RequestMapping("/movie/{movieId}?append_to_response=videos%2Cwatch%2Fproviders&api_key=cd999edfe5283dd7798a09f49f851c0c&language=en-US&page={page}")
    fun getMovieMetaData(@PathVariable movieId: Int): JsonNode

}

@Component
class FetchMetaDataProcessor : ItemProcessor<MovieDto, MovieDto> {

    @Autowired
    lateinit var moviesRemote: MoviesRemote

    override fun process(item: MovieDto): MovieDto? {
        val movieMetaData = moviesRemote.getMovieMetaData(item.id)

        // TODO: Set the metadata on the item

        return item
    }
}
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "3.2.1"
    id("io.spring.dependency-management") version "1.1.4"
    kotlin("jvm") version "1.9.20"
    kotlin("plugin.spring") version "1.9.20"
//    kotlin("plugin.jpa") version "1.9.20"
}

group = "be.robbevw"
version = "0.0.1-SNAPSHOT"

java {
    sourceCompatibility = JavaVersion.VERSION_17
}

repositories {
    mavenCentral()
}

extra["springCloudVersion"] = "2023.0.0"

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-batch")
    implementation("org.apache.commons:commons-compress:1.21")
//    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.16.1")
    implementation("org.springframework.cloud:spring-cloud-starter-openfeign")
    runtimeOnly("org.postgresql:postgresql")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.batch:spring-batch-test")
}

dependencyManagement {
    imports {
        mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
    }
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs += "-Xjsr305=strict"
        jvmTarget = "17"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

I have already tried to add the @Component annotation to the reader.

I have added the @EnableFeignClients annotation and added the base package as well.


Solution

  • The issue was that I was initializing the FetchMetaDataProcessor in my StepBuilder factory manually by creating the object manually.

    The solution was to let Spring autowire this processor in the batch config file. IntelliJ stills throws an error, but the application works now.