springboot中如何在propertes配置文件中加list和map并读取

  • Post author:
  • Post category:其他

1,自定义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

2 设置一个类去读取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<>();

}

3,使用,(直接取值)

@Autowired
    private CityCodeConfig cityCodeConfig;
   
   public class test(){
   
   		cityCodeConfig.getList().
   }


版权声明:本文为weixin_45980070原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。