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();
            }
        }
    }
  • 相关阅读:
    hash算法
    TCP/IP四层与OSI七层模型
    di
    VSCode安装程序——java开发
    java中的多线程
    C#ThreadPool类—多线程
    学习-思考
    DataTable通过Select进行过滤
    javascript遍历对象属性
    WebClient 与HttpClient 的区别
  • 原文地址:https://www.cnblogs.com/redhat0019/p/11697296.html
Copyright © 2011-2022 走看看