yml文件解析

YamlPropertiesFactoryBean

在开发过程中,遇到一个yml文件解析的功能,随手做个记录。
具体要求是这样的,前段传递一个key,后端通过这个key获取配置文件中的key对应着值,若是用properties文件的话,是比较好获取的,但是yml文件的话,通过我的各种尝试,最终用org.springframework.beans.factory.config.YamlPropertiesFactoryBean这个类解决了解析yml文件的问题
代码如下:

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

那么我们在传入key时一定要传一个完整的key过去,比如server.tomcat.uri-encoding,若只传递server.tomcat,则获取到的是null.
当然spring还提供了YamlMapFactoryBean。这个对象可以帮助你获取一个Map<String, Object>。

{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<String, Object>,然后每个key对应的value里面又以子节点构造一个Map<String, Object>。
比如上面的代码中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}

依次嵌套。
这两个解析yml文件的类都是spring自带的,用的很方便,可以根据自己的需求来自己选择。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值