Spring Boot Binder java.util.NoSuchElementException

54 views Asked by At

Initially I wrote a question about NoSuchElementException that gave me crazy, but after a day I could success it, so I have created a demo for somebody that has the same error and wants to see a working example.

https://github.com/danipenaperez/environment-processors

Basically, show how to read properties and bind to java objects using Spring Boot Binders.

Configuration like this:

app:
  testing:
    car:  #Simple object Mapping
      name: Mercedes
      color: white
    drivers: # Map of objects
      daniel:
        age: 12
      susan:
        age: 15
    sponsors:
      - mahou beers
      - burguer kong
      - awesome sponsor

Will be easily binder to a java object

public class TestingConfigurationProperties {
    
    private Car car;  //Simple object Mapping
    
    private Map<String, Driver> drivers; //Map of objects
    
    private List<String> sponsors; //List of objects

    ...
}

Procesor will parse config and inyect the created bean to context:

@Configuration
public class Processors implements BeanDefinitionRegistryPostProcessor, BeanPostProcessor , EnvironmentAware {
    ....
    ...
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        var tc = Binder.get(environment).bind("app.testing", TestingConfigurationProperties.class).get();

        beanFactory.registerSingleton("raceCarProperties", tc);
   }
...
} 

As said, there is a working example https://github.com/danipenaperez/environment-processors

0

There are 0 answers