Spring SpEL - 用于创建字符串和自定义对象映射的表达式语言
Spring SpEL - Expression Language to create Map of String and Custom Object
我正在使用 Spring Boot 示例从属性文件中读取以下内容。
sub.region.data={\
AF: {'subRegionCd' : '34', 'subRegionName' : 'Southern Asia', 'subRegionDesc': '', 'status' : 'A'} \
}
我在下面用过,但是不行
@Value("#{${sub.region.data}}")
private Map<String, SubRegion> subRegionsMap;
SubRegion.java
public class SubRegion {
private String subRegionCd;
private String subRegionName;
private String subRegionDesc;
private String subRegionStatus;
}
我低于错误
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.util.Collections$UnmodifiableMap' to required type 'java.util.Map'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.util.Collections$UnmodifiableMap' to required type 'com.xxxxxx.model.SubRegion': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:76) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1195) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
... 54 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.util.Collections$UnmodifiableMap' to required type 'com.xxxxxx.model.SubRegion': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:262) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:608) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:182) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:73) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
... 57 common frames omitted
看起来你在 'subRegionDesc',
之后犯了一个错误,我想你的意思是在这里使用冒号,而不是逗号
使用 spring 引导我建议您使用 ConfigurationProperties,而不是 @Value
。
例如,在这种情况下您必须:
将 @EnableConfigurationProperties(SubRegionConfig.class)
放入您的 spring 配置之一 class。
创建配置 class:
@ConfigurationProperties(prefix = "sub.region")
public static class SubRegionConfig {
private Map<String, SubRegion> data;
//getters and setters
}
用.yml
代替.properties
,像这样:
sub:
region:
data:
AF:
subRegionCd: '34'
subRegionName: 'Southern Asia'
subRegionDesc: ''
subRegionStatus: 'A'
之后你可以从SubRegionConfing
得到你想要的每一个属性
@Autowired
private SubRegionConfig subRegionConfig;
ConfigurationsProperties
更复杂,但更灵活,在大多数情况下更适合使用。
#{} 运算符将数据发送到 spEL 解析器,这意味着它将尝试根据 spEL 语法将数据转换为其已知类型 here在 Spring 文档中。
编译器无法将任何数据转换为 SubRegion 对象:
Cannot convert value of type 'java.util.Collections$UnmodifiableMap'
to required type 'com.xxxxxx.model.SubRegion': no matching editors or
conversion strategy found
一个简单的解决方法是嵌套两个地图对象,这样您就可以在检索值后填充您的子区域对象。
yourMap={'key1':{'nestedMapKey1:value', 'nestedMapKey2:value2'}}
在包含代码的 .properties 文件中:
sub.region.data={'AF':{'subRegionCd':'34', 'subRegionName':'Southern Asia', 'subRegionDesc':'', 'status':'A'}}
然后,在您的代码中:
@Value("#{${sub.region.data}}")
private Map<String, Map<String, String>> subRegionsMap;
// (...) handling the SubRegion
我的System.out.println(subRegionMap.get("AF");输出:
{subRegionCd=34, subRegionName=Southern Asia, subRegionDesc=, status=A}
我正在使用 Spring Boot 示例从属性文件中读取以下内容。
sub.region.data={\
AF: {'subRegionCd' : '34', 'subRegionName' : 'Southern Asia', 'subRegionDesc': '', 'status' : 'A'} \
}
我在下面用过,但是不行
@Value("#{${sub.region.data}}")
private Map<String, SubRegion> subRegionsMap;
SubRegion.java
public class SubRegion {
private String subRegionCd;
private String subRegionName;
private String subRegionDesc;
private String subRegionStatus;
}
我低于错误
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.util.Collections$UnmodifiableMap' to required type 'java.util.Map'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.util.Collections$UnmodifiableMap' to required type 'com.xxxxxx.model.SubRegion': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:76) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1195) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
... 54 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.util.Collections$UnmodifiableMap' to required type 'com.xxxxxx.model.SubRegion': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:262) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:608) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:182) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:73) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
... 57 common frames omitted
看起来你在 'subRegionDesc',
之后犯了一个错误,我想你的意思是在这里使用冒号,而不是逗号
使用 spring 引导我建议您使用 ConfigurationProperties,而不是 @Value
。
例如,在这种情况下您必须:
将
@EnableConfigurationProperties(SubRegionConfig.class)
放入您的 spring 配置之一 class。创建配置 class:
@ConfigurationProperties(prefix = "sub.region") public static class SubRegionConfig { private Map<String, SubRegion> data; //getters and setters }
用
.yml
代替.properties
,像这样:sub: region: data: AF: subRegionCd: '34' subRegionName: 'Southern Asia' subRegionDesc: '' subRegionStatus: 'A'
之后你可以从
得到你想要的每一个属性SubRegionConfing
@Autowired private SubRegionConfig subRegionConfig;
ConfigurationsProperties
更复杂,但更灵活,在大多数情况下更适合使用。
#{} 运算符将数据发送到 spEL 解析器,这意味着它将尝试根据 spEL 语法将数据转换为其已知类型 here在 Spring 文档中。
编译器无法将任何数据转换为 SubRegion 对象:
Cannot convert value of type 'java.util.Collections$UnmodifiableMap' to required type 'com.xxxxxx.model.SubRegion': no matching editors or conversion strategy found
一个简单的解决方法是嵌套两个地图对象,这样您就可以在检索值后填充您的子区域对象。
yourMap={'key1':{'nestedMapKey1:value', 'nestedMapKey2:value2'}}
在包含代码的 .properties 文件中:
sub.region.data={'AF':{'subRegionCd':'34', 'subRegionName':'Southern Asia', 'subRegionDesc':'', 'status':'A'}}
然后,在您的代码中:
@Value("#{${sub.region.data}}")
private Map<String, Map<String, String>> subRegionsMap;
// (...) handling the SubRegion
我的System.out.println(subRegionMap.get("AF");输出:
{subRegionCd=34, subRegionName=Southern Asia, subRegionDesc=, status=A}