在springboot应用程序的application.properties中配置映射

时间:2018-06-27 09:01:50

标签: java spring-boot properties hashmap application.properties

我正在开发一个 spring boot 应用程序,在其中我正在application.properties中配置属性。我的问题是如何在.properties文件中配置 map 属性。对于 String ,它可以正常工作,但不适用于 map

@Service(value = "myService")
@PropertySource(value = "classpath:application.properties")
@EnableConfigurationProperties
public class MyService
{
    @Value("${my.map.config}")
    private Map mapProperty;

public Map<String, String> getMapProperty()
    {
        return mapProperty;
    }

    public void setMapProperty(Map<String, String> mapProperty)
    {
        this.mapProperty = mapProperty;
    }
}

application.properties:

my.map.config.key1 = value1
my.map.config.key2 = value2

我还尝试如下更改我的application.properties文件,但仍然无法正常工作:

my.map.config = {key1: 'value1', key2: 'value2'}

错误:

  

由于:java.lang.IllegalStateException:无法转换以下项的值:   将“ java.lang.String”键入所需的“ java.util.Map”类型:无匹配项   找到编辑者或转换策略

让我知道如何通过application.properties文件使它工作吗?

0 个答案:

没有答案
相关问题