yml文件解析
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
