Springboot读取配置文件,实现给种类型的映射
比如映射成对象,映射成Map,List等等, 并且支持自动注入,方便使用
- properties文件
spring.application.name=properties-demo
#方式1 ------ list<Student>属性接收
#前缀.list属性字段名[下标].对象的属性名
properties.demo.students[0].id=1
properties.demo.students[0].name=xiaoHong
properties.demo.students[0].sex=0
properties.demo.students[0].age=16
properties.demo.students[1].id=2
properties.demo.students[1].name=xiaoGang
properties.demo.students[1].sex=1
properties.demo.students[1].age=17
#方式2 ------ map<String,Student>属性接收
# 前缀.map属性字段名.map-key.map-对象的属性名
properties.demo.studentMap.xiaoMing.id=3
properties.demo.studentMap.xiaoMing.name=xiaoMing
properties.demo.studentMap.xiaoMing.sex=1
properties.demo.studentMap.xiaoMing.age=15
properties.demo.studentMap.xiaoHong.id=5
properties.demo.studentMap.xiaoHong.name=xiaoHong
properties.demo.studentMap.xiaoHong.sex=0
properties.demo.studentMap.xiaoHong.age=14
#方式3 ------ 对象属性接收
#前缀.对象属性名
properties.demo.student.id=1
properties.demo.student.name=hanMeiMei
properties.demo.student.sex=0
properties.demo.student.age=12
#方式4 ------ 解析为list
properties.demo.strList[0]=10
properties.demo.strList[1]=11
properties.demo.strList[2]=12
#方式5 ------ 前缀.map属性字段名.map-key = map-value
properties.demo.single-map.develop=7
properties.demo.single-map.office=8
properties.demo.sso-map.develop=9
properties.demo.sso-map.office=10
#方式6 ------ List<Map<String,List<String>>>类型 配置
properties.demo.movements[0].reading=zs,ls,ww
properties.demo.movements[1].play-basketball=zl,cz,rg
properties.demo.movements[2].swim=vg
properties.demo.Learn[0].reading=uj
properties.demo.Learn[1].to-sing=zs,okl
properties.demo.Learn[2].play-piano=ll
#方式7 ------ 对象直接接收
#前缀.对象属性名
properties.object.demo.id=7
properties.object.demo.name=LiLei
properties.object.demo.sex=1
properties.object.demo.age=16
#方式8 ------ 使用 @Value 注解直接接收为List
properties.test.string-list=1,2,3
- 实体类Student和StudentTest
@Data
public class Student {
private String id;
private String name;
private int sex;
private int age;
}
@Data
@Component
@ConfigurationProperties(prefix = "properties.object.demo")
public class StudentTest {
private String id;
private String name;
private int sex;
private int age;
}
- 集合 List< Student >
@Data
@Component
@ConfigurationProperties(prefix = "properties.demo")
public class StudentConfig {
//方式1
private List<Student> students = new ArrayList<>();
//方式2
private Map<String, Student> studentMap = new HashMap<>();
//方式3
private Student student;
//方式4
private List<String> strList = new ArrayList<>();
//方式5
private Map<String, String> singleMap = new HashMap<>();
private Map<String, String> ssoMap = new HashMap<>();
//方式6
private List<Map<String, List<String>>> movements = new ArrayList<>();
private List<Map<String, List<String>>> Learn = new ArrayList<>();
}
- 代码中自动注入,并使用
@Component
public class DemoService {
@Autowired
private StudentConfig studentConfig;
//方式7
@Autowired
private StudentTest studentTest;
//方式8
@Value("#{'${properties.test.string-list}'.split(',')}")
private List<Integer> stringList;
public void printMsg() {
List<Student> students = studentConfig.getStudents();
for (Student student : students) {
System.out.println(student);
}
System.out.println("====== 方式1 ======\n");
Map<String, Student> studentMap = studentConfig.getStudentMap();
studentMap.forEach((k, v) -> {
System.out.println("key:" + k + " value:" + v);
});
System.out.println("====== 方式2 ======\n");
Student student = studentConfig.getStudent();
System.out.println(student);
System.out.println("====== 方式3 ======\n");
List<String> strList = studentConfig.getStrList();
System.out.println(strList);
System.out.println("====== 方式4 ======\n");
Map<String, String> singleMap = studentConfig.getSingleMap();
Map<String, String> ssoMap = studentConfig.getSsoMap();
System.out.println("singleMap: "+singleMap);
System.out.println("ssoMap: "+ssoMap);
System.out.println("====== 方式5 ======\n");
List<Map<String, List<String>>> movements = studentConfig.getMovements();
List<Map<String, List<String>>> learn = studentConfig.getLearn();
System.out.println("movements: "+movements);
System.out.println("learn: "+learn);
System.out.println("====== 方式6 ======\n");
System.out.println(studentTest);
System.out.println("====== 方式7 ======\n");
System.out.println(stringList);
System.out.println("====== 方式8 ======\n");
}
}
- 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class PropertiesDemoApplicationTests {
@Autowired
private DemoService demoService;
@Test
public void testProperties() {
demoService.printMsg();
}
}
- 输出结果
Student(id=1, name=xiaoHong, sex=0, age=16)
Student(id=2, name=xiaoGang, sex=1, age=17)
====== 方式1 ======
key:xiaoMing value:Student(id=3, name=xiaoMing, sex=1, age=15)
key:xiaoHong value:Student(id=5, name=xiaoHong, sex=0, age=14)
====== 方式2 ======
Student(id=1, name=hanMeiMei, sex=0, age=12)
====== 方式3 ======
[10, 11, 12]
====== 方式4 ======
singleMap: {develop=7, office=8}
ssoMap: {develop=9, office=10}
====== 方式5 ======
movements: [{reading=[zs, ls, ww]}, {play-basketball=[zl, cz, rg]}, {swim=[vg]}]
learn: [{reading=[uj]}, {to-sing=[zs, okl]}, {play-piano=[ll]}]
====== 方式6 ======
StudentTest(id=7, name=LiLei, sex=1, age=16)
====== 方式7 ======
[1, 2, 3]
====== 方式8 ======