title: Spring-Boot: how to bind custom nested application properties to @ConfigurationProperties class?
body: <p>I'm trying to add some custom application properties to my Spring-Boot project.
  They look like this:</p>
  
  <pre><code>application:
      kafka:
          topic1: topicA
          topic2: topicB
  </code></pre>
  
  <p>And this is the <code>@ConfigurationProperties</code> class:</p>
  
  <pre><code>@ConfigurationProperties(prefix = "application", ignoreUnknownFields = false)
  public class ApplicationProperties {
  
      public static class Kafka {
          private String topic1;
          private String topic2;
  
          public String getTopic1() {
              return topic1;
          }
  
          public void setTopic1(String topic1) {
              this.topic1 = topic1;
          }
  
          public String getTopic2() {
              return topic2;
          }
  
          public void setTopic2(String topic2) {
              this.topic2 = topic2;
          }
      }
  }
  </code></pre>
  
  <p>When I start the application I get the following error:</p>
  
  <pre><code>Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
  2018-11-16 16:13:04 [restartedMain] ERROR o.s.boot.SpringApplication - Application startup failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'application-com.project.config.ApplicationProperties': Could not bind properties to ApplicationProperties (prefix=application, ignoreInvalidFields=false, ignoreUnknownFields=false, ignoreNestedProperties=false); nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'kafka[topic1]' of bean class [com.project.config.ApplicationProperties]: Cannot access indexed value in property referenced in indexed property path 'kafka[topic1]'; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'kafka[topic1]' of bean class [com.project.config.ApplicationProperties]: Bean property 'kafka[topic1]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
      at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:336)
      at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:292)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1626)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
      at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
      at com.project.projectApp.main(projectApp.java:68)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
  Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'kafka[topic1]' of bean class [com.project.config.ApplicationProperties]: Cannot access indexed value in property referenced in indexed property path 'kafka[topic1]'; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'kafka[topic1]' of bean class [com.project.config.ApplicationProperties]: Bean property 'kafka[topic1]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
      at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyHoldingValue(AbstractNestablePropertyAccessor.java:403)
      at org.springframework.beans.AbstractNestablePropertyAccessor.processKeyedProperty(AbstractNestablePropertyAccessor.java:296)
      at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:287)
      at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:278)
      at org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanWrapper.setPropertyValue(RelaxedDataBinder.java:726)
      at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
      at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:860)
      at org.springframework.validation.DataBinder.doBind(DataBinder.java:756)
      at org.springframework.boot.bind.RelaxedDataBinder.doBind(RelaxedDataBinder.java:137)
      at org.springframework.validation.DataBinder.bind(DataBinder.java:741)
      at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:287)
      at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:250)
      at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:331)
      ... 22 common frames omitted
  Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'kafka[topic1]' of bean class [com.project.config.ApplicationProperties]: Bean property 'kafka[topic1]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
      at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:631)
      at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyHoldingValue(AbstractNestablePropertyAccessor.java:400)
      ... 34 common frames omitted
  </code></pre>
  
  <p>How was it supposed to be declared inside my ApplicationProperties in order to be correctly binded?</p>