zoukankan      html  css  js  c++  java
  • 读取配置文件信息

    1. 需要导入 common-configuration2 包

    <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-configuration2</artifactId>
                <version>2.6</version>
            </dependency>
            <dependency>
                <groupId>commons-beanutils</groupId>
                <artifactId>commons-beanutils</artifactId>
                <version>1.9.4</version>
            </dependency>

    2. 配置文件信息 例如web工程中 resource中的文件 application.properties 文件

    # Jedis
        #jedis的 host 名称
    jedis.host=hadoop1
    
    # Mongodb
        #mongo的host名称
    mongo.host=hadoop1
        #mongo的port端口
    mongo.port=27017

    3. 代码实现

    package com.atguigu.server.utils;
    
    import org.apache.commons.configuration2.Configuration;
    import org.apache.commons.configuration2.builder.fluent.Configurations;
    import org.apache.commons.configuration2.ex.ConfigurationException;
    import java.io.File;
    
    public class Test {
        public static void main(String[] args) {
            Configurations configs = new Configurations();
            try {
                Configuration config = configs.properties(new File("application.properties"));
                String redisHost = config.getString("jedis.host");
                System.out.println(redisHost);
            } catch (ConfigurationException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    会议安排最优算法
    Python Singleton
    Android HandlerThread 源代码分析
    [Android]_[0基础]_[adb 有用命令]
    使用sshfs将远程目录挂载到本地
    Netty 中ChannelOption的含义以及使用的场景
    netty4.0 Server和Client的通信
    Netty重要概念介绍
    Zookeeper单机伪集群
    整数集合
  • 原文地址:https://www.cnblogs.com/redhat0019/p/11697296.html
Copyright © 2011-2022 走看看