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:

@Component
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")
		userController.getUser()
		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!

HOW DO I DO

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

@Configuration
class JobConfiguration {
	@Bean
	fun commentJob(userController: UserController) = CommentJob(userController)
}

or:

@Configuration
class JobConfiguration(private val userController: UserController) {
	@Bean
	fun commentJob() = CommentJob(userController)
}
//@Component
class IExternalService<T >(private val services: Services) : AbstractService<T> {

    @Autowired
    lateinit var template: RestTemplate

    @Autowired
    private lateinit var retryTemplate: RetryTemplate

And that’s my @Bean here

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

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

        val retryPolicy = SimpleRetryPolicy()
        retryPolicy.maxAttempts = maxAttempts
        retryTemplate.setRetryPolicy(retryPolicy)

        return retryTemplate
    }

The old way, autowire from constructor parameters

@Component
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
) {