Currently I try to rewrite my Java Spring Boot Application with Kotlin. I encountered a problem that in all of my classes which are annotated with @Service
the dependency injection is not working correctly (all instances are null
). Here is an example:
@Service
@Transactional
open class UserServiceController @Autowired constructor(val dsl: DSLContext, val teamService: TeamService) {
//dsl and teamService are null in all methods
}
Doing the same in Java works without any problems:
@Service
@Transactional
public class UserServiceController
{
private DSLContext dsl;
private TeamService teamService;
@Autowired
public UserServiceController(DSLContext dsl,
TeamService teamService)
{
this.dsl = dsl;
this.teamService = teamService;
}
If I annotate the component with @Component
in Kotlin everything works fine:
@Component
open class UserServiceController @Autowired constructor(val dsl: DSLContext, val teamService: TeamService) {
//dsl and teamService are injected properly
}
Google provided many different approaches for Kotlin and @Autowired
which I tried but all resulted in the same NullPointerException
I would like to know what the difference between Kotlin and Java is and how I can fix this?