SpringBoot 官方文档示例:(27)通过application.properties给Map对象注入属性
一、在配置类中定义Map属性package cn.edu.tju.config;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;import java.util.HashMap;
·
一、在配置类中定义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中配置的第四个属性的反斜线被去掉了
更多推荐
所有评论(0)