ymlファイル解析

4821 ワード

ymlファイル解析
  • Yaml PropertiesFactoryBean
  • テスト
  • 注意
  • Yaml PropertiesFactoryBen
    開発中にymlファイル解析の機能がありましたら、メモしてください.具体的には、前のキーを転送して、バックエンドはこのkeyで設定ファイルの中のkeyに対応しています.propertiesファイルを使うと、入手しやすいですが、ymlファイルは私の様々な試みを通じて、最終的にorg.springframe ewark.beans.factory.com fig.Yaml Propties FactoryBenというクラスで解析した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はYaml MapFactoryBenも提供します.このオブジェクトはあなたがMapを取得するのを助けることができます.
    {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}}
    
    以上のように、これはYaml MapFactoryBeanを通じて得られた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}
    
    順番に入れ子するこの二つの解析のymlファイルの種類はすべてspringが持っているので、使いやすいです.自分の需要に応じて自分で選ぶことができます.