[英]Does Spring's Environment Abstraction use PropertyEditors?
My google-fu is failing me on this one. 我的谷歌福使我失败了。
I'm using Spring 4.2.4.RELEASE with java configuration. 我在Java配置中使用Spring 4.2.4.RELEASE。 What I'm trying to do is register a custom property editor to convert from a String to a Map. 我正在尝试做的是注册一个自定义属性编辑器,以将String转换为Map。
So I have a Java Configuration class that registers the appropriate BeanFactoryPostProcessor 所以我有一个Java Configuration类,用于注册适当的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 the same configuration class I am also injecting the Environment
在同一配置类中,我还要注入Environment
@Resource
private Environment environment;
However, when I try to get the String property (which is also unfortunately named environment
) that I want converted to a map, I get an exception. 但是,当我尝试获取要转换为地图的String属性(不幸的是,它也被命名为environment
)时,出现了异常。
environment.getProperty("environment", Map.class, Collections.EMPTY_MAP)
Exception: 例外:
Caused by: java.lang.IllegalArgumentException: Cannot convert value [VAR=hello] from source type [String] to target type [Map]
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:94)
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:65)
at org.springframework.core.env.AbstractPropertyResolver.getProperty(AbstractPropertyResolver.java:143)
at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:546)
BUT if I inject the property directly using the @Value
annotation, it works just fine. 但是,如果我直接使用@Value
批注注入属性,它就可以正常工作。
@Value("${environment}")
private Map<String, String> shellEnvironment;
So, what gives? 那么,有什么用呢? Does the Environment
object not take into account registered property editors? Environment
对象是否不考虑注册的属性编辑器? Do I need to create a custom Converter ? 我需要创建自定义转换器吗? Isn't the Environment
abstraction the latest and greatest way to resolve properties that come from anywhere? Environment
抽象不是解决来自任何地方的属性的最新,最有效的方法吗?
No ; 没有 the Environment abstraction does not use registered Property Editors. 环境抽象不使用注册的属性编辑器。 It only resolves properties (and does basic Type Conversion). 它仅解析属性(并且执行基本的类型转换)。
Thanks to the comments from @M. 感谢@M的评论。 Deinum I was able to come to a decent compromise. Deinum我能够做出不错的妥协。
Correct it is a way to resolve properties it isn't a way to convert properties. 正确,这是解析属性的方法,而不是转换属性的方法。 When using the @Value 2 things happen, first the property is resolved, second conversion is attempted (only basic conversion String to numbers/booleans is done through plain java not converters/editors.). 使用@Value 2时,首先会解析该属性,然后尝试进行第二次转换(仅通过普通Java而不是转换器/编辑器完成基本的字符串到数字/布尔值的转换)。 You only do the resolution part not the conversion part. 您只执行分辨率部分,而不执行转换部分。 – M. Deinum – Deinum M
You have to kind of read between the lines in the reference manual to come to this understanding. 您必须仔细阅读参考手册中的内容才能获得这种理解。
The Environment Abstraction chapter states: 环境抽象一章指出:
The role of the Environment object with relation to properties is to provide the user with a convenient service interface for configuring property sources and resolving properties from them . 环境对象与属性有关的作用是为用户提供方便的服务界面,用于配置属性源并从中解析属性 。
Notice it does not say anything about conversion/editors. 请注意,它没有提及转换/编辑器。 (Although I did notice that it does actually handle simple Type Conversion because Environment inherits PropertyResolver.getPropertyAsClass ) (尽管我确实注意到它实际上处理了简单的类型转换,因为Environment继承了PropertyResolver.getPropertyAsClass )
So I decided to combine the two methods and settled on this method of injecting my properties into my configuration class: 因此,我决定将这两种方法结合起来,并决定使用将属性注入配置类的这种方法:
@Resource
private Environment environment;
@Value("#{environment['environment']?:{:}}")
private Map<String, String> shellEnvironment;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.