@Values 与集合Map

kadbb459  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(310)

这个问题在这里已经有答案了

如何使用@value-spring注解注入Map(7个答案)
22天前关门了。
我有以下代码:

@Value("#{${optional.currencies}}") 
private Map<Account, Set<String>> mapAccountAndCurrencies= null;

但我有以下例外:

Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.util.Collections$UnmodifiableRandomAccessList' to required type 'java.util.Map'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.util.Collections$UnmodifiableRandomAccessList' to required type 'java.util.Map': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:76)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1235)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:636)
    ... 68 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.util.Collections$UnmodifiableRandomAccessList' to required type 'java.util.Map': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:262)
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:73)
    ... 71 more
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBatchTest
@EnableAutoConfiguration
@ContextConfiguration(classes= {BatchTestConfiguration.class})
@TestPropertySource(locations="classpath:application-test.properties")
public class BatchTest {

}

以及我的applicatin-test.properties:

optional.currencies={}

我做错什么了?

2cmtqfgy

2cmtqfgy1#

你需要设置 optional.currencies 成为一张集合的Map,就像

optional.currencies={'account1': {'USD','GBP'}, 'account2': {'USD','NOK'}}

你能告诉我这是否解决了你的问题吗?

af7jpaap

af7jpaap2#

试试这个-

@Value("#{${optional.currencies:{null:null}}}") 
private Map<Account, Set<String>> mapAccountAndCurrencies= null;

相关问题