自定义citycode.properties
#List properties
citycode.list[0]=www
citycode.list[1]=localhost
citycode.list[2]=wuhan
citycode.list[3]=tianjin
#Map Properties
citycode.map.www=4201
citycode.map.wuhan=4201
citycode.map.tianjin=1200
设置一个类去读取properties
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
@Configuration
@PropertySource("classpath:citycode.properties")
@ConfigurationProperties(prefix = "citycode")
public class CityCodeConfig {
private List<String> list = new ArrayList<>();
private Map<String, String> map = new HashMap<>();
}
注意properties文件的属性命名必须全部小写或以中横线分割,绝对不能出现大写字母.
网友评论