Search code examples
spring-bootkotlinspring-data-r2dbc

SpringBoot @Autowire not working inside @Repository


I have a multi module SpringBoot app with gradle and kotlin, trying spring-data-r2dbc. If i use @Repository annotation over my Repository class, @Autowired DatabaseClient is null. But if i change annotation to @Component, @Autowired works and I do successfull call to database. Any idea why @Autowire isn't working with @Repository annotation?

Database configuration class:

@Configuration
open class DatabaseConfiguration(
    @Value("\${spring.data.mssql.host}") private val host: String,
   // @Value("\${spring.data.mssql.port}") private val port: Int,
    @Value("\${spring.data.mssql.database}") private val database: String,
    @Value("\${spring.data.mssql.username}") private val username: String,
    @Value("\${spring.data.mssql.password}") private val password: String)
: AbstractR2dbcConfiguration() {

@Bean
override fun connectionFactory(): ConnectionFactory {
    return MssqlConnectionFactory(
            MssqlConnectionConfiguration.builder()
                    .host(host)
                    //.port(port)
                    .database(database)
                    .username(username)
                    .password(password).build()
    )
 }
}

Main class:

@SpringBootApplication
@EnableR2dbcRepositories
class MultigradleApplication

Repository class (in module "data"):

@Repository
open class TestRepo() {

@Autowired
lateinit var client: DatabaseClient

    fun getAll() : Flux<PersonDTO> {
            return client.execute("SELECT * FROM Person.Person")
                .`as`(PersonDTO::class.java)
                .fetch()
                .all()
    }
}

Solution

  • The problem was that I injected PersonService as its IPersonService interface into my Controller, but I didn't have an interface for TestRepo and injected it directly. When I added ITestRepo interface to TestRepo and injected it into the service as ITestRepo, everything started working.