一、在配置类中定义Map属性

package cn.edu.tju.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@ConfigurationProperties(prefix = "my.test")
public class AppConfig4 {
    private Map span;

    public Map<String, Integer> getSpan() {
        return span;
    }

    public void setSpan(Map<String, Integer> span) {
        this.span = span;
    }
}

二、在application.properties中配置map中的属性,如果属性名中包含如下字符以外的字符,则需要用中括号【】括起来。

my.test.span.[/first]=1
my.test.span.[second]=18
my.test.span.[/third]=30
my.test.span./fourth=40

其中span这个map中包含四个属性:
/first
second
/third
fourth
注意,application.properties中配置的第四个属性的反斜线被去掉了

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐