Spring SpEL-用于创建字符串和自定义对象映射的表达语言

我正在使用 示例从属性文件中读取以下内容。

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 Boot" title="Spring Boot">Spring Boot,我建议您使用ConfigurationProperties而不是@Value

例如,在这种情况下,您必须:

  1. 放入@EnableConfigurationProperties(SubRegionConfig.class)您的spring配置类之一。

  2. 创建配置类:

        @ConfigurationProperties(prefix = "sub.region")

public static class SubRegionConfig {

private Map<String, SubRegion> data;

//getters and setters

}

  1. 使用.yml代替.properties,例如:

        sub:

region:

data:

AF:

subRegionCd: '34'

subRegionName: 'Southern Asia'

subRegionDesc: ''

subRegionStatus: 'A'

  1. 之后,您可以从中获得所需的所有属性。 SubRegionConfing

        @Autowired

private SubRegionConfig subRegionConfig;

ConfigurationsProperties 在大多数情况下更复杂,但更易于使用。

以上是 Spring SpEL-用于创建字符串和自定义对象映射的表达语言 的全部内容, 来源链接: utcz.com/qa/411095.html

回到顶部