SpringBoot gets the contents of the yml and properties configuration files

  • 2020-06-23 00:15:37
  • OfStack

(1) yml profile:

pom.xml adds dependency:


<!--  support  @ConfigurationProperties  annotations  -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
   <version>${spring-boot.version}</version>
</dependency>

Add the following to the application.yml file:


# Custom properties and values 
myYml:
 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

Use 1 java class to get the contents of the yml file:


package com.sun.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  loading yaml Methods for configuration files 
 * Created by sun on 2017-1-15.
 * spring-boot Update to the 1.5.2 After the version locations Attribute cannot be used 
 * @PropertySource Annotations can only be loaded proprties file , Unable to load yaml file 
 *  So let's put the data in application.yml In the file ,spring-boot Load on startup 
 */
@Component
//@ConfigurationProperties(locations = {"classpath:config/myProps.yml"},prefix = "myProps")
@ConfigurationProperties(prefix = "myYml")
public class YmlConfig {

  String simpleProp;
  private String[] arrayProps;
  private List<Map<String, String>> listProp1 = new ArrayList<>(); // receive prop1 The value of the property inside 
  private List<String> listProp2 = new ArrayList<>(); // receive prop2 The value of the property inside 
  private Map<String, String> mapProps = new HashMap<>(); // receive prop1 The value of the property inside 

  public String getSimpleProp() {
    return simpleProp;
  }

  //String The type of 1 Will need to setter To receive the value of the property; maps, collections,  and  arrays  Don't need 
  public void setSimpleProp(String simpleProp) {
    this.simpleProp = simpleProp;
  }

  public String[] getArrayProps() {
    return arrayProps;
  }

  public void setArrayProps(String[] arrayProps) {
    this.arrayProps = arrayProps;
  }

  public List<Map<String, String>> getListProp1() {
    return listProp1;
  }

  public void setListProp1(List<Map<String, String>> listProp1) {
    this.listProp1 = listProp1;
  }

  public List<String> getListProp2() {
    return listProp2;
  }

  public void setListProp2(List<String> listProp2) {
    this.listProp2 = listProp2;
  }

  public Map<String, String> getMapProps() {
    return mapProps;
  }

  public void setMapProps(Map<String, String> mapProps) {
    this.mapProps = mapProps;
  }
}

This object can be obtained by dependency injection:


@Autowired
private YmlConfig config;

Method to obtain values:


ObjectMapper objectMapper = new ObjectMapper();
// The test load yml file 
System.out.println("simpleProp: " + config.getSimpleProp());
System.out.println("arrayProps: " + objectMapper.writeValueAsString(config.getArrayProps()));
System.out.println("listProp1: " + objectMapper.writeValueAsString(config.getListProp1()));
System.out.println("listProp2: " + objectMapper.writeValueAsString(config.getListProp2()));
System.out.println("mapProps: " + objectMapper.writeValueAsString(config.getMapProps()));

(2) properties profile:

The @ES28en annotation was used to load the configuration file. The annotation could not load the yml configuration file. Use the @Value annotation to get the parameter values in the file


package com.sun.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

/**
 *  loading properties The configuration file , Is available in the method 
 * abc.properties File does not exist , validation ignoreResourceNotFound attribute 
 *  add encoding = "utf-8" Property to prevent Chinese scrambling , It can't be capitalized "UTF-8"
 * Created by sun on 2017-3-30.
 */
@Configuration
@PropertySource(value = {"classpath:/config/propConfigs.properties","classpath:/config/abc.properties"},
    ignoreResourceNotFound = true,encoding = "utf-8")
public class PropConfig {

  // PropertySourcesPlaceholderConfigurer this bean . 
  //  this bean Mainly used to solve problems @value Used in the ${ ... } Placeholder. 
  //  Suppose you don't use it ${ ... } If you're a placeholder, you don't have to use this bean . 
  @Bean
  public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
  }
}
// To obtain properties There are two methods for file parameter values, 1 Kind of get Environment  Object of, and 2 Kind of is @Value annotations 

@Autowired
  private Environment env;
  @Value("${age}")
  String name;


  @RequestMapping("/")
  @ResponseBody
  String home(HttpServletRequest req) throws JsonProcessingException, UnsupportedEncodingException {
    logger.info(" The test passed!! ");
    ObjectMapper objectMapper = new ObjectMapper();
    // The test load yml file 
    System.out.println("simpleProp: " + config.getSimpleProp());
    System.out.println("arrayProps: " + objectMapper.writeValueAsString(config.getArrayProps()));
    System.out.println("listProp1: " + objectMapper.writeValueAsString(config.getListProp1()));
    System.out.println("listProp2: " + objectMapper.writeValueAsString(config.getListProp2()));
    System.out.println("mapProps: " + objectMapper.writeValueAsString(config.getMapProps()));

    // The test load properties file 
    System.out.println(env.getProperty("name"));// Sun kai 
    System.out.println(env.getProperty("abc"));//null
    System.out.println(name);//26

    return "Hello World!";
  }


Related articles: