SpringBoot获取yml和属性配置文件的内容

  

<强>(一)yml配置文件:

  

pom.xml加入依赖:

        & lt; !——支持@ConfigurationProperties注解——比;   & lt; !——https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor——比;   & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-configuration-processor   & lt; version> $ {spring-boot.version} & lt;/version>   & lt;/dependency>   之前      

在application.yml文件中加上:

        #自定义的属性和值   myYml:   simpleProp: simplePropValue   arrayProps: 1、2、3、4、5所示   listProp1:   名称:美国广播公司   价值:abcValue   ——名称:efg   价值:efgValue   listProp2:   ——config2Value1   ——config2Vavlue2   mapProps:   key1: value1   key2: value2   之前      

使用一个java类获取yml文件的内容:

        包com.sun.configuration;      进口org.springframework.boot.context.properties.ConfigurationProperties;   进口org.springframework.stereotype.Component;      进口java.util.ArrayList;   进口java.util.HashMap;   进口并不知道;   进口java.util.Map;/* *   *加载yaml配置文件的方法   *由太阳>   @ autowired   私人YmlConfig配置;   之前      

方法内获取值:

        objectmap objectmap=new objectmap ();//测试加载yml文件   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 ()));   之前      

<强>(二)属性配置文件:

  

使用@PropertySource注解加载配置文件,该注解无法加载yml配置文件。使用@ value注解获得文件中的参数值

        包com.sun.configuration;      进口org.springframework.context.annotation.Bean;   进口org.springframework.context.annotation.Configuration;   进口org.springframework.context.annotation.PropertySource;   进口org.springframework.context.support.PropertySourcesPlaceholderConfigurer;/* *   *加载属性配置文件,在方法中可以获取   * abc.properties文件不存在,验证ignoreResourceNotFound属性   *加上编码=" utf - 8 "属性防止中文乱码,不能为大写的“utf - 8”   *由太阳2017-3-30。   */@ configuration   @PropertySource (value=https://www.yisu.com/zixun/{“类路径:/config/propConfigs.properties”、“类路径:/config/abc.properties "},   ignoreResourceNotFound=true,编码=皍tf - 8”)   公开课PropConfig {//PropertySourcesPlaceholderConfigurer这个豆,//这个bean主要用于解决@ value中使用的${…}占位符。//假如你不使用${…}占位符的话,可以不使用这个bean。   @ bean   公共静态PropertySourcesPlaceholderConfigurer PropertySourcesPlaceholderConfigurer () {   返回新PropertySourcesPlaceholderConfigurer ();   }   }//获取属性文件参数值有两种方法,一种获得环境的对象,第二种就是@ value注解      @ autowired   私人环境env;   @ value(" ${时代}")   字符串名称;         @RequestMapping (“/?   @ResponseBody   字符串(HttpServletRequest点播)抛出JsonProcessingException, UnsupportedEncodingException {   logger.info(“测试通过! ! !”);   objectmap objectmap=new objectmap ();//测试加载yml文件   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 ()));//测试加载属性文件   System.out.println (env.getProperty (" name ");//孙凯   System.out.println (env.getProperty (abc));//null   System.out.println(名字);//26      返回“Hello World !”;   }      之前      

SpringBoot获取yml和属性配置文件的内容