全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

本文实例为大家分享了SpringBoot获取yml和properties配置文件的具体代码,供大家参考,具体内容如下

(一)yml配置文件:

pom.xml加入依赖:

<!-- 支持 @ConfigurationProperties 注解 -->
<!-- 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>

在application.yml文件中加上:

#自定义的属性和值
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

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

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;

/**
 * 加载yaml配置文件的方法
 * Created by sun on 2017-1-15.
 * spring-boot更新到1.5.2版本后locations属性无法使用
 * @PropertySource注解只可以加载proprties文件,无法加载yaml文件
 * 故现在把数据放到application.yml文件中,spring-boot启动时会加载
 */
@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<>(); //接收prop1里面的属性值
  private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的属性值
  private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的属性值

  public String getSimpleProp() {
    return simpleProp;
  }

  //String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要
  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;
  }
}

通过依赖注入就可以获取该对象:

@Autowired
private YmlConfig config;

方法内获取值:

ObjectMapper objectMapper = new ObjectMapper();
//测试加载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()));

(二)properties配置文件:

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

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;

/**
 * 加载properties配置文件,在方法中可以获取
 * abc.properties文件不存在,验证ignoreResourceNotFound属性
 * 加上encoding = "utf-8"属性防止中文乱码,不能为大写的"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这个bean,
  // 这个bean主要用于解决@value中使用的${…}占位符。
  // 假如你不使用${…}占位符的话,可以不使用这个bean。
  @Bean
  public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
  }
}
//获取properties文件参数值有两种方法,一种获得Environment 的对象,第二种就是@Value注解

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


  @RequestMapping("/")
  @ResponseBody
  String home(HttpServletRequest req) throws JsonProcessingException, UnsupportedEncodingException {
    logger.info("测试通过!!!");
    ObjectMapper objectMapper = new ObjectMapper();
    //测试加载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()));

    //测试加载properties文件
    System.out.println(env.getProperty("name"));//孙凯
    System.out.println(env.getProperty("abc"));//null
    System.out.println(name);//26

    return "Hello World!";
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# SpringBoot  # yml  # properties  # 在SpringBoot下读取自定义properties配置文件的方法  # Spring Boot 日志配置方法(超详细)  # SpringBoot + Spring Security 基本使用及个性化登录配置详解  # springboot如何读取配置文件(application.yml)中的属性值  # 详解SpringBoot配置连接池  # spring boot Logging的配置以及使用详解  # spring boot的maven配置依赖详解  # spring boot开发遇到坑之spring-boot-starter-web配置文件使用教程  # Springboot配置doris连接的实现示例  # 加载  # 配置文件  # 不需要  # 你不  # 不存在  # 自定义  # 有两种  # 大家分享  # 主要用于  # 第二种  # 使用这个  # 具体内容  # 大家多多  # 为大  # 就可以  # 新到  # mapProps  # simplePropValue  # abc  # package 


相关文章: 如何通过网站建站时间优化SEO与用户体验?  网站制作报价单模板图片,小松挖机官方网站报价?  如何用VPS主机快速搭建个人网站?  湖州网站制作公司有哪些,浙江中蓝新能源公司官网?  建站上传速度慢?如何优化加速网站加载效率?  如何自定义建站之星模板颜色并下载新样式?  如何选择长沙网站建站模板?H5响应式与品牌定制哪个更优?  高端建站三要素:定制模板、企业官网与响应式设计优化  北京的网站制作公司有哪些,哪个视频网站最好?  广东企业建站网站优化与SEO营销核心策略指南  建站之星代理费用多少?最新价格详情介绍  北京网站制作网页,网站升级改版需要多久?  新网站制作渠道有哪些,跪求一个无线渠道比较强的小说网站,我要发表小说?  如何将凡科建站内容保存为本地文件?  如何在IIS7上新建站点并设置安全权限?  代刷网站制作软件,别人代刷火车票靠谱吗?  IOS倒计时设置UIButton标题title的抖动问题  MySQL查询结果复制到新表的方法(更新、插入)  如何通过商城自助建站源码实现零基础高效建站?  C++用Dijkstra(迪杰斯特拉)算法求最短路径  建站之星logo尺寸如何设置最合适?  Dapper的Execute方法的返回值是什么意思 Dapper Execute返回值详解  建站之星如何配置系统实现高效建站?  c# await 一个已经完成的Task会发生什么  网站专业制作公司有哪些,做一个公司网站要多少钱?  如何通过宝塔面板实现本地网站访问?  ,网站推广常用方法?  如何打造高效商业网站?建站目的决定转化率  如何做静态网页,sublimetext3.0制作静态网页?  高防服务器租用指南:配置选择与快速部署攻略  香港服务器建站指南:外贸独立站搭建与跨境电商配置流程  郑州企业网站制作公司,郑州招聘网站有哪些?  如何制作一个表白网站视频,关于勇敢表白的小标题?  公司网站建设制作费用,想建设一个属于自己的企业网站,该如何去做?  网站视频怎么制作,哪个网站可以免费收看好莱坞经典大片?  北京企业网站设计制作公司,北京铁路集团官方网站?  专业的网站制作设计是什么,如何制作一个企业网站,建设网站的基本步骤有哪些?  大连网站设计制作招聘信息,大连投诉网站有哪些?  如何在IIS中新建站点并解决端口绑定冲突?  建站之星导航如何优化提升用户体验?  如何在万网自助建站平台快速创建网站?  php json中文编码为null的解决办法  网站制作免费,什么网站能看正片电影?  如何通过虚拟主机快速搭建个人网站?  建站之星2.7模板快速切换与批量管理功能操作指南  图片制作网站免费软件,有没有免费的网站或软件可以将图片批量转为A4大小的pdf?  胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?  建站之星官网登录失败?如何快速解决?  建站之星如何实现PC+手机+微信网站五合一建站?  如何快速建站并高效导出源代码? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。