Kotlin lateinit Autowired issue in spring boot

I’m not able to get bean in spring boot with autowired annotation.

I just described my full issue in the following stackoverflow:

Stackover flow link

Please help me to fix this

I wouldn’t bother with lateinit if I was in your place. Just inject dependencies via constructor:

class CommentJob(private val userController: UserController) : Job {
	private val logger = LoggerFactory.getLogger(CommentJob::class.java)
	override fun execute(context: JobExecutionContext) {
		logger.debug("Inside Comment Job")
		logger.debug("End Comment Job")
1 Like

Thanks let me check this

@madmax1028 If I Inject via Constructor That also not working. its throws instantiationexception any idea

Can’t say for sure without more context. From what I see most likely something is trying to create an instance using no-arg constructor, which is no present here obliviously, but the thing is that Spring should normally use our constructor and inject dependencies even without @Autowired as long as it’s the only available construtor.

It’s almost like the Spring wasn’t used at all. It may somewhat explain why you also got the previous exception using lateinit. An instance was created without any concern for dependencies.

So Now how can I fix this? Stackoverflow and other internet medium d’not have more information about this issue.

Hi! your way work, it’s fine. But what if there’s really a need to manually autowire a bean?

for example, in case right now i’m try to manually autowire a restTemplate bean from a class anotated with @Component. before, i did has you recommend to do, everything was fine. but now i need to manualy autowire my beans, but @Autowired seems not working, i just got a null with my beans!


Manually autowire? Not sure if I understood correctly, but maybe you meant this:

class JobConfiguration {
	fun commentJob(userController: UserController) = CommentJob(userController)


class JobConfiguration(private val userController: UserController) {
	fun commentJob() = CommentJob(userController)
class IExternalService<T >(private val services: Services) : AbstractService<T> {

    lateinit var template: RestTemplate

    private lateinit var retryTemplate: RetryTemplate

And that’s my @Bean here

    fun configureRestTemplate(objectMapper: ObjectMapper): RestTemplate {
        val restTemplate = RestTemplate(this.initClientHttpRequestFactory())
        replaceRestTemplateDefaultObjectMapper(restTemplate, objectMapper)
        return restTemplate

    fun configureRetryTemplate(): RetryTemplate {
        val retryTemplate = RetryTemplate()
        val fixedBackOffPolicy = FixedBackOffPolicy()
        fixedBackOffPolicy.backOffPeriod = backOffPolicy

        val retryPolicy = SimpleRetryPolicy()
        retryPolicy.maxAttempts = maxAttempts

        return retryTemplate

The old way, autowire from constructor parameters

class AuthenticationExternalService(
        private val restTemplate: RestTemplate,
        private val retryTemplate: RetryTemplate,
        @Value("\${service.authentication.base.url}") private val authenticationUrl: String,
        @Value("\${service.authentication.info.url}") private val userInfoUrl: String,
        @Value("\${service.authentication.reset.url}") private val resetPasswordUrl: String,
        @Value("\${service.authentication.update.password.url}") private val updatePasswordUrl: String
) {