yml文件解析

yml文件解析yml 文件解析 YamlProperti 测试注意 YamlProperti 在开发过程中 遇到一个 yml 文件解析的功能 随手做个记录 具体要求是这样的 前段传递一个 key 后端通过这个 key 获取配置文件中的 key 对应着值 若是用 properties 文件的话 是比较好获取的 但是 yml 文件的话 通过我的各种尝试 最终用 org springframe

YamlPropertiesFactoryBean

import java.util.Properties; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.core.io.ClassPathResource; / * yml文件解析工具 * * @author chunhui.tan * @version 创建时间:2018年10月10日 上午10:57:12 * */ public class YmlUtils { private static final String FILENAME = "application.yml"; / * 获取yml文件中的值 * * @param fileName yml文件名,需要在claspath目录下,classpath下直接传文件名, * 否则需要传递classpath下的相对路径,默认application.yml * @param typeName yml文件中的key值 * @return */ public static String getTypePropertie(String fileName, String typeName) { YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean(); if (!StringUtils.isNotBlank(fileName)) { fileName = FILENAME; } yaml.setResources(new ClassPathResource(fileName)); Properties properties = yaml.getObject(); return properties.getProperty(typeName); } } 

测试

yml文件内容如下:

server: tomcat: uri-encoding: UTF-8 max-threads: 1000 min-spare-threads: 30 port: 8088 servlet: context-path: /test 

测试代码:

public static void main(String[] args) { String propertie = getTypePropertie(null,"server.port"); System.out.println(propertie); } 

结果:

15:20:06.516 [main] DEBUG org.springframework.beans.factory.config.YamlPropertiesFactoryBean - Loading from YAML: class path resource [application.yml]
15:20:06.557 [main] DEBUG org.springframework.beans.factory.config.YamlPropertiesFactoryBean - Merging document (no matchers set): {server={tomcat={uri-encoding=UTF-8, max-threads=1000, min-spare-threads=30}, port=8088, servlet={context-path=/xquant}}, spring={profiles={active=dev}, jackson={date-format=yyyy-MM-dd HH:mm:ss, time-zone=GMT+8}, servlet={multipart={max-file-size=100MB, max-request-size=100MB, enabled=true}}, freemarker={suffix=.html, request-context-attribute=request}}, mybatis-plus={mapper-locations=classpath*:mapper//*.xml, typeAliasesPackage=com.xQuant.app.modules.*.entity, global-config={id-type=0, key-generator=com.baomidou.mybatisplus.incrementer.OracleKeyGenerator, field-strategy=2, db-column-underline=true, refresh-mapper=true, logic-delete-value=-1, logic-not-delete-value=0, sql-injector=com.baomidou.mybatisplus.mapper.LogicSqlInjector}, configuration={map-underscore-to-camel-case=true, cache-enabled=false, call-setters-on-nulls=true, jdbc-type-for-null=null}}, test={name=zhangsan}}
15:20:06.557 [main] DEBUG org.springframework.beans.factory.config.YamlPropertiesFactoryBean - Loaded 1 document from YAML resource: class path resource [application.yml]
8088

注意

yml配置文件中的配置项是一层一层的,比如测试中的配置项完全可以改成properties的形式:

server.tomcat.uri-encoding=utf-8 server.tomcat.max-threads=1000 server.tomcat.min-spare-threads=30 server.port=8088 server.servlet.context-path=/test 
{server={tomcat={uri-encoding=UTF-8, max-threads=1000, min-spare-threads=30}, port=8088, servlet={context-path=/xquant}}, spring={profiles={active=dev}, jackson={date-format=yyyy-MM-dd HH:mm:ss, time-zone=GMT+8}, servlet={multipart={max-file-size=100MB, max-request-size=100MB, enabled=true}}, freemarker={suffix=.html, request-context-attribute=request}}, mybatis-plus={mapper-locations=classpath*:mapper//*.xml, typeAliasesPackage=com.xQuant.app.modules.*.entity, global-config={id-type=0, key-generator=com.baomidou.mybatisplus.incrementer.OracleKeyGenerator, field-strategy=2, db-column-underline=true, refresh-mapper=true, logic-delete-value=-1, logic-not-delete-value=0, sql-injector=com.baomidou.mybatisplus.mapper.LogicSqlInjector}, configuration={map-underscore-to-camel-case=true, cache-enabled=false, call-setters-on-nulls=true, jdbc-type-for-null=null}}, test={name=zhangsan}}

如上,这是通过YamlMapFactoryBean获取到的map对象,他是以根节点为key构造的一个Map

,然后每个key对应的value里面又以子节点构造一个Map



比如上面的代码中server这个根节点对应一个大的map:




{tomcat={uri-encoding=UTF-8, max-threads=1000, min-spare-threads=30}, port=8088, servlet={context-path=/xquant}} 

里面的tomcat又对应一个map:

{uri-encoding=UTF-8, max-threads=1000, min-spare-threads=30} 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211291.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午10:46
下一篇 2026年3月18日 下午10:46


相关推荐

  • 通配符掩码的应用 ACL 访问控制列表

    通配符掩码的应用 ACL 访问控制列表ACL(AccessControlList)访问控制列表在作为数据包的过滤器以及在对指定的某种类型的数据包的优先级,起到了对某些数据包的优先级起到了限制流量的作用,减少了网络的拥塞。通配符掩码作为ACL中重要的一部分,是路由器在进行访问控制时必不可少的重要部件,那么什么是通配符掩码呢?通配符掩码:路由器使用通配符掩码与原地址或者是目标地址一起来分辨匹配的地址范围,在访问控制列表中,将通…

    2022年7月19日
    17
  • CountDownTimer_bytebuffer slice

    CountDownTimer_bytebuffer slicepublicclassCountDownTimerManager{/***总倒计时*/privatelongmillisInFuture=0;/***回调时间*/privatelongcountDownInterval;/***倒计时完成回调*/privateFinishCountDownfinishCountDown;/**

    2026年1月17日
    6
  • Java中遍历Map集合的五种方式[通俗易懂]

    包含for循环遍历、entrySet使用iterator遍历,并且介绍了Java8新特性使用lambda表达式forEach遍历。

    2022年4月9日
    65
  • harbor

    harbor

    2021年5月28日
    118
  • C面试题整理(带答案)

    C面试题整理(带答案)C 面试题整理 带答案 1 维护数据库的完整性 一致性 你喜欢用触发器还是自写业务逻辑 为什么 答 尽可能用约束 包括 CHECK 主键 唯一键 外键 非空字段 实现 这种方式的效率最好 其次用触发器 这种方式可以保证无论何种业务系统访问数据库都能维持数据库的完整性 一致性 最后再考虑用自写业务逻辑实现 但这种方式效率最低 编程最复杂 当为下下之策 2 什么是事务 什么是锁 答 事务

    2026年3月27日
    1
  • 动态规划之背包问题及输出背包具体方案[通俗易懂]

    动态规划之背包问题及输出背包具体方案[通俗易懂]题型1:LintCode92.背包问题题目:在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i]。分析:dp[i][j]:当背包总重量为j且目前有i个物品时,背包最多装满dp[i][j]的空间。      状态转移方程为:dp[i][j]=max{dp[i-1][j-A[i-1]]+A[i-1],dp[i-1][j]},其中dp[i-1][j-A[…

    2022年7月26日
    15

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号