我正在使用 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',,我认为您的意思是使用冒号,而不是逗号
'subRegionDesc',
通过Spring Boot,我建议您使用ConfigurationProperties而不是@Value。
@Value
例如,在这种情况下,您必须:
放入@EnableConfigurationProperties(SubRegionConfig.class)您的spring配置类之一。
@EnableConfigurationProperties(SubRegionConfig.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 在大多数情况下更复杂,但更易于使用。
ConfigurationsProperties