java - Does Spring's Environment Abstraction use PropertyEditors? -


my google-fu failing me on one.

i'm using spring 4.2.4.release java configuration. i'm trying register custom property editor convert string map.

so have java configuration class registers appropriate beanfactorypostprocessor

@bean public static customeditorconfigurer customeditorconfigurer(){     customeditorconfigurer configurer = new customeditorconfigurer();     map<class<?>, class<? extends propertyeditor>> customeditors = new hashmap<>();     customeditors.put(map.class, delimitedstringtomappropertyeditor.class);     configurer.setcustomeditors(customeditors);       return configurer; } 

in same configuration class injecting environment

@resource private environment environment; 

however, when try string property (which unfortunately named environment) want converted map, exception.

environment.getproperty("environment", map.class, collections.empty_map) 

exception:

caused by: java.lang.illegalargumentexception: cannot convert value [var=hello] source type [string] target type [map] @ org.springframework.core.env.propertysourcespropertyresolver.getproperty(propertysourcespropertyresolver.java:94) @ org.springframework.core.env.propertysourcespropertyresolver.getproperty(propertysourcespropertyresolver.java:65) @ org.springframework.core.env.abstractpropertyresolver.getproperty(abstractpropertyresolver.java:143) @ org.springframework.core.env.abstractenvironment.getproperty(abstractenvironment.java:546) 

but if inject property directly using @value annotation, works fine.

@value("${environment}") private map<string, string> shellenvironment; 

so, gives? environment object not take account registered property editors? need create custom converter? isn't environment abstraction latest , greatest way resolve properties come anywhere?

no; environment abstraction not use registered property editors. resolves properties (and basic type conversion).


thanks comments @m. deinum able come decent compromise.

correct way resolve properties isn't way convert properties. when using @value 2 things happen, first property resolved, second conversion attempted (only basic conversion string numbers/booleans done through plain java not converters/editors.). resolution part not conversion part. – m. deinum

you have kind of read between lines in reference manual come understanding.

the environment abstraction chapter states:

the role of environment object relation properties provide user convenient service interface configuring property sources , resolving properties them.

notice not conversion/editors. (although did notice handle simple type conversion because environment inherits propertyresolver.getpropertyasclass)

so decided combine 2 methods , settled on method of injecting properties configuration class:

@resource private environment environment;  @value("#{environment['environment']?:{:}}") private map<string, string> shellenvironment; 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo