Springboot读取配置文件,映射成对象或者属性

Springboot读取配置文件,实现给种类型的映射
比如映射成对象,映射成Map,List等等, 并且支持自动注入,方便使用

  1. 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

  1. 实体类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;
}

  1. 集合 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<>();

}

  1. 代码中自动注入,并使用
@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");

    }
}

  1. 测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class PropertiesDemoApplicationTests {

    @Autowired
    private DemoService demoService;

    @Test
    public void testProperties() {
        demoService.printMsg();
    }
}

  1. 输出结果
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  ======
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot提供了很方便的方式来读取配置文件。你可以使用`@Value`注解来直接注入配置值,或者使用`@ConfigurationProperties`注解将配置文件的值映射到一个POJO类中。 首先,确保在你的`application.properties`或`application.yml`文件中定义了配置项。例如,假设你希望读取一个名为`app.name`的属性,可以在配置文件中添加以下内容: ```properties app.name=MyApp ``` 然后,在你的Spring Boot应用中,你可以使用`@Value`注解来直接注入配置值。例如,你可以在一个组件类中使用以下代码: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MyComponent { @Value("${app.name}") private String appName; // 其他代码... } ``` 这样,`appName`字段将被注入为配置文件中`app.name`对应的值。 另一种方式是使用`@ConfigurationProperties`注解。首先,创建一个POJO类,与配置文件中的属性一一对应。例如: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "app") public class MyAppProperties { private String name; // 其他属性的getter和setter方法... // getter和setter方法省略... } ``` 在上述代码中,`prefix`属性指定了配置文件属性的前缀,这里是`app`。然后,在需要使用配置值的地方,你可以注入这个POJO类: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MyComponent { private final MyAppProperties appProperties; @Autowired public MyComponent(MyAppProperties appProperties) { this.appProperties = appProperties; } // 使用appProperties中的配置值... // 其他代码... } ``` 通过这种方式,你可以方便地将配置文件中的属性映射到一个POJO类中,并在应用中使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值