title: Spring Boot @Autowired custom application.properties
body: <p>i am trying to get my custon application.properties fields working within a kotlin spring boot application.</p>
  
  <p>My Current Setup:</p>
  
  <p>ApplicationProperties.kt:</p>
  
  <pre><code>@Configuration
  @ConfigurationProperties("mt")
  class ApplicationProperties {
  
      var initDatabase: Boolean? = null
      val kafka = Kafka()
  
      class Kafka {
          lateinit var host: String
          lateinit var port: String
      }
  }
  </code></pre>
  
  <p>application.properties:</p>
  
  <pre><code>mt.kafka.host=localhost
  mt.kafka.port=9092
  mt.init-database=true
  </code></pre>
  
  <p>And a class where i want to use my application:
  InitApp.kt</p>
  
  <pre><code>@Component
  @EnableTransactionManagement
  @EnableConfigurationProperties(ApplicationProperties::class)
  class InitApp {
      @Autowired
      lateinit var conf: ApplicationProperties
  
      @Bean
      fun createKafkaConsumer() {
          log.info("${conf.kafka.host}")
      }
  }
  </code></pre>
  
  <p>Sadly this doesn't work because i get a:</p>
  
  <pre><code>Caused by: kotlin.UninitializedPropertyAccessException: lateinit property conf has not been initialized
  </code></pre>
  
  <p>I followed this "guide" <a href="https://youtu.be/btNIey_2Zdw?t=1580" rel="nofollow noreferrer">Bootiful Kotlin by Sébastien Deleuze and Josh Long @ Spring I/O 2018</a> and i am using these versions:</p>
  
  <pre><code>    kotlinVersion = '1.2.51'
      springBootVersion = '2.0.5.RELEASE'
  </code></pre>
  
  <p><code>apply plugin: 'kotlin-kapt'</code> is in my build.gradle, but this should not mess with the <code>@Autowired</code> since, as far as i understood, it is for autocompletion in my application.properties.</p>
  
  <h1>Update 1:</h1>
  
  <p>I had a look at: <a href="https://spring.io/guides/tutorials/spring-boot-kotlin/" rel="nofollow noreferrer">Building web applications with Spring Boot and Kotlin</a> and added the <code>@EnableConfigurationProperties</code> at Application level. This didn't work either</p>
  
  <h1>Update  2:</h1>
  
  <p>Full stacktrace:</p>
  
  <pre><code>2018-10-10 16:17:53.058 ERROR 23619 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed
  
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'producerFactory' defined in class path resource [de/mikatiming/de/test/InitApp.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.kafka.core.ProducerFactory]: Factory method 'producerFactory' threw exception; nested exception is kotlin.UninitializedPropertyAccessException: lateinit property applicationProperties has not been initialized
  at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:590) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1247) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1096) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
  at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1277) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1265) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
  at de.mikatiming.de.test.TestApplicationKt.main(TestApplication.kt:15) [main/:na]
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_172]
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_172]
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_172]
  at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_172]
  at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.5.RELEASE.jar:2.0.5.RELEASE]
  Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.kafka.core.ProducerFactory]: Factory method 'producerFactory' threw exception; nested exception is kotlin.UninitializedPropertyAccessException: lateinit property applicationProperties has not been initialized
  at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  ... 23 common frames omitted
  Caused by: kotlin.UninitializedPropertyAccessException: lateinit property applicationProperties has not been initialized
  at de.mikatiming.de.test.InitApp.getApplicationProperties(InitApp.kt:36) ~[main/:na]
  at de.mikatiming.de.test.InitApp.producerFactory(InitApp.kt:58) ~[main/:na]
  at de.mikatiming.de.test.InitApp$$EnhancerBySpringCGLIB$$c3b698bb.CGLIB$producerFactory$10(&lt;generated&gt;) ~[main/:na]
  at de.mikatiming.de.test.InitApp$$EnhancerBySpringCGLIB$$c3b698bb$$FastClassBySpringCGLIB$$b315799f.invoke(&lt;generated&gt;) ~[main/:na]
  at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:365) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  at de.mikatiming.de.test.InitApp$$EnhancerBySpringCGLIB$$c3b698bb.producerFactory(&lt;generated&gt;) ~[main/:na]
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_172]
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_172]
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_172]
  at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_172]
  at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
  ... 24 common frames omitted
  </code></pre>
  
  <h1>Update 3:</h1>
  
  <p>I just found out that when i remove a specific bean from my InitApp:</p>
  
  <pre><code>@Bean
  fun placeHolderConfigurer(): PropertySourcesPlaceholderConfigurer {
      return PropertySourcesPlaceholderConfigurer()
  }
  </code></pre>
  
  <p>i get an other error message:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'de.mikatiming.messageengine.configuration.ApplicationProperties' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  </code></pre>
  
  <p>So it looks like it is an issue with this bean!</p>