1. 获取.properties/.yml方法

注意事项: 在 .yml 配置时,冒号(:)后面一定要有空格, 没有空格可能会有一下情况

  • 使用Environment和封装类时,值为null
  • 使用@Value时,spring boot项目启动时会报:org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘webController’: Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder ‘test.testMsg’ in value “${test.testMsg}”

1. 方法一:使用@Value(“”)

2. 方法二:使用Environment

  1. package com.rit.demo.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.core.env.Environment;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. @RequestMapping("/webCon")
  9. public class WebController {
  10. @Value("${test.testMsg}")
  11. private String msg;
  12. @Autowired
  13. private Environment env;
  14. @RequestMapping("/index1")
  15. public String index1() {
  16. return "方式一:"+msg;
  17. }
  18. @RequestMapping("/index2")
  19. public String index2() {
  20. return "方式二:"+env.getProperty("redis.province.redisKey");
  21. }
  22. }
  1. #application.properties
  2. redis.province.redisKey=witkeyProvince

正确配置:image.png
错误配置:image.png

3. 方法三:使用封装类

  1. 创建application.yml
  2. 引入依赖
  3. 创建bean
  4. 创建测试类
    1. #application.yml
    2. myProps: #自定义的属性和值
    3. simpleProp: simplePropValue
    4. arrayProps: 1,2,3,4,5
    5. listProp1:
    6. - name: abc
    7. value: abcValue
    8. - name: efg
    9. value: efgValue
    10. listProp2:
    11. - config2Value1
    12. - config2Vavlue2
    13. mapProps:
    14. key1: value1
    15. key2: value2
    ```xml
    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-configuration-processor</artifactId>
    4. <optional>true</optional>
    5. </dependency>
  1. ```java
  2. //bean.class
  3. package com.rit.demo.pojo;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import org.springframework.boot.context.properties.ConfigurationProperties;
  9. import org.springframework.stereotype.Component;
  10. @Component
  11. //接收application.yml中的wechat下面的屬性
  12. @ConfigurationProperties(prefix = "my-props")
  13. public class ReadApplicationUntil {
  14. private String simpleProp;
  15. private String[] arrayProps;
  16. private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的属性值
  17. private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的属性值
  18. private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的属性值
  19. public String getSimpleProp() {
  20. return simpleProp;
  21. }
  22. //String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要
  23. public void setSimpleProp(String simpleProp) {
  24. this.simpleProp = simpleProp;
  25. }
  26. public List<Map<String, String>> getListProp1() {
  27. return listProp1;
  28. }
  29. public List<String> getListProp2() {
  30. return listProp2;
  31. }
  32. public String[] getArrayProps() {
  33. return arrayProps;
  34. }
  35. public void setArrayProps(String[] arrayProps) {
  36. this.arrayProps = arrayProps;
  37. }
  38. public Map<String, String> getMapProps() {
  39. return mapProps;
  40. }
  41. public void setMapProps(Map<String, String> mapProps) {
  42. this.mapProps = mapProps;
  43. }
  44. }
  1. //测试类
  2. package com.rit.demo.test;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import com.fasterxml.jackson.core.JsonProcessingException;
  7. import com.fasterxml.jackson.databind.ObjectMapper;
  8. import com.rit.demo.pojo.ReadApplicationUntil;
  9. @SpringBootTest
  10. public class YmlTest {
  11. private final ObjectMapper objectMapper = new ObjectMapper();
  12. @Autowired
  13. private ReadApplicationUntil applicationUntil;
  14. @Test
  15. public void yml() throws JsonProcessingException {
  16. System.out.println("simpleProp: " + applicationUntil.getSimpleProp());
  17. System.out.println("arrayProps: " + objectMapper.writeValueAsString(applicationUntil.getArrayProps()));
  18. System.out.println("listProp1: " + objectMapper.writeValueAsString(applicationUntil.getListProp1()));
  19. System.out.println("listProp2: " + objectMapper.writeValueAsString(applicationUntil.getListProp2()));
  20. System.out.println("mapProps: " + objectMapper.writeValueAsString(applicationUntil.getMapProps()));
  21. }
  22. }

测试结果: simpleProp: simplePropValue arrayProps: [“1”,”2”,”3”,”4”,”5”] listProp1: [{“name”:”abc”,”value”:”abcValue”},{“name”:”efg”,”value”:”efgValue”}] listProp2: [“config2Value1”,”config2Vavlue2”]

mapProps: {“key1”:”value1”,”key2”:”value2”}

补充知识:

如何执行测试类:
image.png