Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@ConfigurationProperties to inject environment properties into a structure of nested objects #6204

Closed
s-bortolussi opened this issue Jun 22, 2016 · 2 comments
Labels
status: duplicate A duplicate of another issue

Comments

@s-bortolussi
Copy link

s-bortolussi commented Jun 22, 2016

I use @ConfigurationPropertiesto inject environment properties into a structure of nested objects.

@Component
@ConfigurationProperties
public class ServiceProperties {

    private Map<String,Service> services;

    public Map<String, Service> getServices() {
        return services;
    }

    public void setServices(Map<String, Service> services) {
        this.services = services;
    }

}

public class Service {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Setting env property as follows fails

SERVICES_DEV_NAME=a name

with following stacktrace

Caused by: org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 1 errors
Field error in object 'target' on field 'services[DEV_NAME]': rejected value [a name]; codes [typeMismatch.target.services[DEV_NAME],typeMismatch.target.services,typeMismatch.services[DEV_NAME],typeMismatch.services,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.services[DEV_NAME],services[DEV_NAME]]; arguments []; default message [services[DEV_NAME]]]; default message [Failed to convert property value of type [java.lang.String] to required type [com.example.Service] for property 'services[DEV_NAME]'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.example.Service] for property 'services[DEV_NAME]': no matching editors or conversion strategy found]
    at org.springframework.boot.bind.PropertiesConfigurationFactory.validate(PropertiesConfigurationFactory.java:362)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:271)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:241)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:334)
    ... 48 more

Can be reproduce using following code.

Notice that, setting env property as follows succeed, but raises compatiblity issues across shells

SERVICES[DEV]_NAME=a name

What should be the format of env variable when dealing with Spring Boot Configuration Binding of Map/nested properties ?

Thanks

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 22, 2016
@philwebb
Copy link
Member

Unfortunately the square brackets are a requirement for map based binding (see #5315). The best work-around we can suggest so far is to use SPRING_APPLICATION_JSON="..." as an environment variable, with the value being a json structure to process. See org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor for details of how it works.

@s-bortolussi
Copy link
Author

@philwebb thanks for the quick reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants