Kotlin object not get auto-injected
Recently I started working on a Spring Boot Kotlin project.
I have been using Spring Boot with Java in the past, and I love its simple and flexible design, especially the autowire function.
Autowiring makes every module loosely coupled from others, which is one of the most powerful features in Spring Boot.
However, I created a new Service class and injected it into another service, as shown in the code below:
@Service |
But the autowire was not applied, coolService in MyController was null.
Can you figure out why?
I searched the web but found no answer.
Yes, ChatGPT listed many reasons, but none of them were correct.
Finally, I changed CoolService from object to class.
It worked!
Spring Boot should output a warning if it cannot autowire an object.
