Spring boot 通过@ConfigurationProperties和@PropertySource读取自定义配置文件信息
程序员文章站
2022-03-02 19:00:49
...
1.自定义配置文件
######
#服装
etl.map[10]=服装
etl.map[11]=春秋服
etl.map[12]=半袖衬衣
etl.map[13]=冬罩衣
etl.map[14]=棉衣
etl.map[15]=制式短裤
etl.map[16]=针织背心
#鞋
etl.map[20]=鞋
etl.map[21]=单鞋
etl.map[22]=棉鞋
#床上用品
etl.map[30]=床上用品
etl.map[31]=床单
etl.map[32]=棉被
etl.map[33]=棉褥
etl.map[34]=被罩
etl.map[35]=枕套
etl.map[36]=枕套枕巾
#配饰
etl.map[40]=配饰
etl.map[41]=皮带
etl.map[42]=棉帽
etl.map[44]=单帽
#其他
etl.map[90]=其他
#list
etl.list[0]=a
etl.list[1]=b
etl.list[2]=c
2.java config
package com.example.demo;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import lombok.Data;
/**
* 读取配置文件
* @author Bowin
*
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "etl",ignoreUnknownFields = false)
@PropertySource(value ={"file:${user.dir}/config/custom.properties","file:${user.dir}/config/custom_*.properties"}, ignoreResourceNotFound = true)
public class PropertiesConfig {
private Map<String,String> map;
private List<String> list;
private Map<String,String> *;//*
}
3.测试
package com.example.demo;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner{
@Autowired
private PropertiesConfig propertiesConfig;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... arg0) throws Exception {
Map<String, String> map = propertiesConfig.getMap();
System.out.println(map.size());
}
}
上一篇: echo yes or no