程序员的量化交易之路(30)--Cointrader之ConfigUtil(17)

简介:

转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top/

一个完整的系统,必然会涉及到配置文件。配置文件可以是xml、属性文件等形式。大多数而言我们并不需要重写配置读取解析模块,只需要使用开源的即可,这里使用的是apapche.commons.configuration的。

我们这里要说的是Cointrader的ConfigUtil类,它涉及到配置和注解成员之间的赋值等问题。

下面通过代码学习:

package org.cryptocoinpartners.util;

import org.apache.commons.configuration.*;
import org.apache.commons.configuration.tree.OverrideCombiner;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.util.*;


/**
 * @author Tim Olson
 */
@SuppressWarnings("UnusedDeclaration")
public class ConfigUtil {

    public static CombinedConfiguration combined() { return combined; }

    public static PropertiesConfiguration defaults() { return defaultConfig; }
    public static PropertiesConfiguration user() { return userConfig; }
    public static PropertiesConfiguration buildtime() { return buildtimeConfig; }
    public static SystemConfiguration system() { return sysConfig; }
    public static MapConfiguration commandLine() { return clConfig; }


    /**
     * Finds all non-static members tagged with @Config and populates them with the current combined() configuration
     * 额,这个注释是错误的。不仅仅是@Config吧。就是素有的非静态成员的注解
     */
    public static void applyConfiguration( Object instance ) {
        Class<?> cls = instance.getClass();
        for( Field field : cls.getFields() ) {
            Config annotation = field.getAnnotation(Config.class);
            if( annotation != null )
                inject(combined(), instance, field, annotation);
        }
    }


    /**
     * Examines injectee for any setters or fields marked with @Config, then sets those fields to values from the
     * Configuration object.
     */
    public static void applyConfiguration(Object injectee, Configuration config) {
        Class<?> cls = injectee.getClass();
        for( Field field : cls.getFields() ) {
            Config annotation = field.getAnnotation(Config.class);
            if( annotation != null )
                inject((AbstractConfiguration) config, injectee, field, annotation);
        }
    }


    public static void init(String filename, Map<String,String> commandLine ) throws ConfigurationException {
        boolean loadUserPropertiesFile = new File(filename).exists();
        if( !loadUserPropertiesFile  )
            log.warn("Could not find configuration file \"" + filename + "\"");
        clConfig = new MapConfiguration(commandLine);
        sysConfig = new SystemConfiguration();
        if( loadUserPropertiesFile )
            userConfig = new PropertiesConfiguration(filename);
        else
            userConfig = new PropertiesConfiguration();
        URL defaultProps = ConfigUtil.class.getResource("/cointrader-default.properties");//加载配置文件
        if( defaultProps == null )
            throw new ConfigurationException("Could not load cointrader-default.properties");
        defaultConfig = new PropertiesConfiguration(defaultProps);
        URL buildtimeProps = ConfigUtil.class.getResource("/org/cryptocoinpartners/buildtime.properties");
        if( buildtimeProps == null )
            throw new ConfigurationException("Could not load buildtime.properties");
        buildtimeConfig = new PropertiesConfiguration(buildtimeProps);
        combined = buildConfig(Collections.<AbstractConfiguration>emptyList());
        if( log.isDebugEnabled() )
            log.debug("Combined Configuration:\n"+ asString(combined));
    }


    public static CombinedConfiguration forModule(Object... keyValuePairs) {
        if( keyValuePairs.length % 2 != 0 )
            throw new Error("Configuration parameters must be key-value pairs.  Found an odd number.");
        HashMap<String,Object> map = new HashMap<>();
        for( int i = 0; i < keyValuePairs.length; i++ )
            map.put(keyValuePairs[i++].toString(),keyValuePairs[i]);
        return forModule(Collections.singletonList(new MapConfiguration(map)));
    }


    public static CombinedConfiguration forModule(Collection<? extends AbstractConfiguration> moduleConfigs) {
        CombinedConfiguration result = buildConfig(moduleConfigs);
        if( log.isDebugEnabled() )
            log.debug("Module Configuration:\n"+ asString(result));
        return result;
    }


    public static List<String> getPathProperty(String pathProperty) {
        CombinedConfiguration config = combined();
        return getPathProperty(config, pathProperty);
    }


    public static List<String> getPathProperty(CombinedConfiguration config, String pathProperty) {
        String modulePath = config.getString(pathProperty, "");
        List<String> paths = new ArrayList<>(Arrays.asList(modulePath.split(":")));
        paths.add("org.cryptocoinpartners.module");
        paths.remove("");
        return paths;
    }


    private static CombinedConfiguration buildConfig(Collection<? extends AbstractConfiguration> intermediateConfigs) {
        final CombinedConfiguration result = new CombinedConfiguration(new OverrideCombiner());
        result.addConfiguration(buildtimeConfig); // buildtime config cannot be overridden
        result.addConfiguration(clConfig);
        result.addConfiguration(sysConfig);
        for( AbstractConfiguration moduleConfig : intermediateConfigs )
            result.addConfiguration(moduleConfig);
        if( !userConfig.isEmpty() )
            result.addConfiguration(userConfig);
        result.addConfiguration(defaultConfig);
        return result;
    }


    public static String asString(Configuration configuration) {
        StringWriter out = new StringWriter();
        PrintWriter pout = new PrintWriter(out);
        ConfigurationUtils.dump(configuration, pout);
        try {
            pout.close();
            out.close();
        }
        catch( IOException e ) {
            throw new Error(e);
        }
        ArrayList<String> outLines = new ArrayList<>();
        for( String line : out.toString().split("\n") ) {
            if( !isSecret(line) )
                outLines.add(line);
        }
        Collections.sort(outLines);
        return StringUtils.join(outLines,"\n");
    }


    protected static boolean isSecret(String line) {
        return line.contains("password") || line.contains("secret");
    }


    private static void inject(@Nullable Object instance, Field field ) {
        inject(combined(), instance, field, null);
    }


    private static void inject(AbstractConfiguration configuration, @Nullable Object instance, Field field, @Nullable Config configAnnotation ) {
        if( instance == null && !Modifier.isStatic(field.getModifiers()) )//如果instance为null或者该数据成员为静态的,则不可赋值
            return;

        if( Modifier.isFinal(field.getModifiers()) ) {//该数据成员是final修饰的,不可以修改。
            log.warn("Field " + field.getDeclaringClass().getName()+"."+field.getName() + " is tagged with @Config but is declared final.  Config for this field failed.");
            return;
        }
        if( configAnnotation == null )//如果configAnnotation为null,则直接获取field的注解。
            configAnnotation = field.getAnnotation(Config.class);
        String key = null;
        if( configAnnotation != null )
            key = configAnnotation.value();//标注的值作为键
        if( key == null )
            key = field.getName();//如果标注没有值,那么直接拿数据成员的名字作为键
        Config classConfigAnnotation = field.getDeclaringClass().getAnnotation(Config.class);//获取成员声明所在类的注解,即类注解
        if( classConfigAnnotation != null )
            key = classConfigAnnotation.value() + "." + key;//用“.”号连接类注解和成员注解
        Object value = getDynamic(field.getType(), configuration, key);//获取该注解对应配置的值
        if( value != null ) {
            try {
                field.set(instance, value);//将值反射注入到instance对象的改数据成员中。
                if( log.isDebugEnabled() ) {
                    // hide values marked as passwords
                    String printValue = isSecret(key) ? "**-hidden-**" : field.get(instance).toString();
                    log.debug("Set field " + field.getDeclaringClass()
                                                  .getName() + "." + field.getName() + " to " + printValue);
                }
            }
            catch( IllegalAccessException e ) {
                log.error("Could not set config on field " + field.getDeclaringClass().getName() + "." + field.getName(),e);
            }
        }
    }


    public static <T> T getDynamic(Class<T> resultType, AbstractConfiguration configuration, String key) {
        return getDynamic(resultType, configuration, key, null);
    }

//从配置文件中读取属性值
    @SuppressWarnings("unchecked")
    public static <T> T getDynamic(Class<T> resultType, AbstractConfiguration configuration, String key, T defaultValue) {
        if( resultType.isAssignableFrom(String.class) )
            return (T) configuration.getString(key, (String) defaultValue);
        else if( resultType.isAssignableFrom(Boolean.class) || resultType.isAssignableFrom(Boolean.TYPE) )
            return (T) configuration.getBoolean(key, (Boolean) defaultValue);
        else if( resultType.isAssignableFrom(Long.class) || resultType.isAssignableFrom(Long.TYPE) )
            return (T) configuration.getLong(key, (Long) defaultValue);
        else if( resultType.isAssignableFrom(Integer.class) || resultType.isAssignableFrom(Integer.TYPE) )
            return (T) configuration.getInteger(key, (Integer) defaultValue);
        else if( resultType.isAssignableFrom(Short.class) || resultType.isAssignableFrom(Short.TYPE) )
            return (T) configuration.getShort(key, (Short) defaultValue);
        else if( resultType.isAssignableFrom(Byte.class) || resultType.isAssignableFrom(Byte.TYPE) )
            return (T) configuration.getByte(key, (Byte) defaultValue);
        else if( resultType.isAssignableFrom(Double.class) || resultType.isAssignableFrom(Double.TYPE) )
            return (T) configuration.getDouble(key, (Double) defaultValue);
        else if( resultType.isAssignableFrom(Float.class) || resultType.isAssignableFrom(Float.TYPE) )
            return (T) configuration.getFloat(key, (Float) defaultValue);
        else if( resultType.isAssignableFrom(List.class) )
            return (T) configuration.getList(key, (List) defaultValue);
        else if( resultType.isAssignableFrom(BigDecimal.class) )
            return (T) configuration.getBigDecimal(key, (BigDecimal) defaultValue);
        else if( resultType.isAssignableFrom(BigInteger.class) )
            return (T) configuration.getBigInteger(key, (BigInteger) defaultValue);

        throw new IllegalArgumentException("Cannot cast configuration values to "+resultType.getName());
    }


    private static PropertiesConfiguration defaultConfig;
    private static PropertiesConfiguration buildtimeConfig;
    private static PropertiesConfiguration userConfig;
    private static MapConfiguration clConfig;
    private static SystemConfiguration sysConfig;
    private static CombinedConfiguration combined;
    private static Logger log = LoggerFactory.getLogger(ConfigUtil.class);
}
我们首先来分析inject函数,它实现的是一个将配置中值注入到对应对象的成员变量中去。

在这里我们补一下获取一个成员的注解的知识:学习代码如下:

package com.yiibai;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

// declare a new annotation
@Retention(RetentionPolicy.RUNTIME)
@interface Demo {

   String str();

   int val();
}

public class PackageDemo {

   // set values for the annotation
   @Demo(str = "Demo Annotation", val = 100)
   // a method to call in the main
   public static void example() {
      PackageDemo ob = new PackageDemo();

      try {
         Class c = ob.getClass();

         // get the method example
         Method m = c.getMethod("example");

         // get the annotation for class Demo
         Demo annotation = m.getAnnotation(Demo.class);

         // print the annotation
         System.out.println(annotation.str() + " " + annotation.val());
      } catch (NoSuchMethodException exc) {
         exc.printStackTrace();
      }
   }

   public static void main(String args[]) {
      example();
   }
}
让我们来编译和运行上面的程序,这将产生以下结果:

Demo Annotation 100


下面还有一个getResource去读取一个文件:

我们引用(抄袭)一个很好很简单的帖子:http://blog.csdn.net/lcj8/article/details/3502849

class.getResource()的用法
用JAVA获取文件,听似简单,但对于很多像我这样的新人来说,还是掌握颇浅,用起来感觉颇深,大常最经常用的,就是用JAVA的File类,如要取得c:/test.txt文件,就会这样用File file = newFile("c:/test.txt");这样用有什么问题,相信大家都知道,就是路径硬编码,对于JAVA精神来说,应用应该一次成型,到处可用,并且从现实应用来讲,最终生成的应用也会部署到Windows外的操作系统中,对于linux来说,在应用中用了c:/这样的字样,就是失败,所以,我们应该尽量避免使用硬编码,即直接使用绝对路径。 


  在Servlet应用中,有一个getRealPath(String str)的方法,这个方法尽管也可以动态地获得文件的路径,不秘直接手写绝对路径,但这也是一个不被建议使用的方法,那么,我们有什么方法可以更好地获得文件呢? 


     那就是Class.getResource()与Class.getResourceAsStream()方法,但很多人还是不太懂它的用法,因为很多人(比如不久前的我)都不知道应该传怎么样的参数给它,当然,有些人己经用得如火纯青,这些人是不需要照顾的,在此仅给不会或者还不是很熟的人解释一点点。 




比如我们有以下目录 
|--project 
    |--src 
        |--javaapplication 
            |--Test.java 
            |--file1.txt 
        |--file2.txt 
    |--build 
        |--javaapplication 
            |--Test.class 
            |--file3.txt 
        |--file4.txt 


在上面的目录中,有一个src目录,这是JAVA源文件的目录,有一个build目录,这是JAVA编译后文件(.class文件等)的存放目录 
那么,我们在Test类中应该如何分别获得 
file1.txt file2.txt file3.txt file4.txt这四个文件呢? 


首先讲file3.txt与file4.txt 
file3.txt: 
方法一:File file3 = new File(Test.class.getResource("file3.txt").getFile()); 
方法二:File file3 = new File(Test.class.getResource("/javaapplication/file3.txt").getFile()); 
方法三:File file3 = new File(Test.class.getClassLoader().getResource("javaapplication/file3.txt").getFile()); 


file4.txt: 
方法一:File file4 = new File(Test.class.getResource("/file4.txt").getFile()); 
方法二:File file4 = new File(Test.class.getClassLoader().getResource("file4.txt").getFile()); 


很好,我们可以有多种方法选择,但是file1与file2文件呢?如何获得? 
答案是,你只能写上它们的绝对路径,不能像file3与file4一样用class.getResource()这种方法获得,它们的获取方法如下 
假如整个project目录放在c:/下,那么file1与file2的获取方法分别为 
file1.txt 
方法一:File file1 = new File("c:/project/src/javaapplication/file1.txt"); 
方法二:。。。没有 


file2.txt 
方法一:File file2 = new File("c:/project/src/file2.txt"); 
方法二:。。。也没有 


总结一下,就是你想获得文件,你得从最终生成的.class文件为着手点,不要以.java文件的路径为出发点,因为真正使用的就是.class,不会拿个.java文件就使用,因为java是编译型语言嘛 


至于getResouce()方法的参数,你以class为出发点,再结合相对路径的概念,就可以准确地定位资源文件了,至于它的根目录嘛,你用不同的IDEbuild出来是不同的位置下的,不过都是以顶层package作为根目录,比如在Web应用中,有一个WEB-INF的目录,WEB-INF目录里面除了web.xml文件外,还有一个classes目录,没错了,它就是你这个WEB应用的package的顶层目录,也是所有.class的根目录“/”,假如clasaes目录下面有一个file.txt文件,它的相对路径就是"/file.txt",如果相对路径不是以"/"开头,那么它就是相对于.class的路径。。 


还有一个getResourceAsStream()方法,参数是与getResouce()方法是一样的,它相当于你用getResource()取得File文件后,再new InputStream(file)一样的结果 


   


class.getResource("/") --> 返回class文件所在的顶级目录,一般为包名的顶级目录。 --> file:/home/duanyong/workspace/cxxx/xxxx/bin/WEB-INF/classes/ 
class.getResource("/xxx.txt") --> 返回顶级目录下的xxx.txt路径。 file://..../bin/WEB-INF/classes/xxx.txt 


getResource(String path),path是以class文件的顶级目标所在的相对路径。如果顶级目录为classes,在classes/xxx/yyy.txt这样一个文件。取得yyy.txt的语法为:class.getResource("/xxx/yyy.txt"); 


示例代码: 
查看复制到剪切板打印
//取得classes顶级目录下的/xxx/yyy.txt文件   
System.out.println(Test.class.getResource("/xxx/yyy.txt"));   
//取得本class的上路径   
System.out.println(Test.class.getResource(Test.class.getSimpleName() + ".class"));          
[Java] view plaincopy
//取得classes顶级目录下的/xxx/yyy.txt文件  
System.out.println(Test.class.getResource("/xxx/yyy.txt"));  
//取得本class的上路径  
System.out.println(Test.class.getResource(Test.class.getSimpleName() + ".class"));          






结果: 
file:/home/duanyong/workspace/test/bin/WEB-INF/classes/xxx/yyy.txt 
file:/home/duanyong/workspace/test/bin/WEB-INF/classes/cn/duanyong/test/Test.class



相关文章
|
4月前
|
人工智能 安全 测试技术
【AI时代软件项目管理系列】4. AI 时代的软件项目启动:从立项评估到 AI 可行性分析
AI 并没有让瀑布型项目失效,尤其在政企、定制化和交付型项目中,合同范围、预算、里程碑、阶段评审和最终验收仍然需要明确治理。真正需要改变的是传统严格串行的管理方式。AI 时代的瀑布项目应保留阶段、基线和责任机制,同时引入快速反馈、持续验证和 AI/Agent 赋能,让需求、设计、开发、测试和交付形成更高效的闭环,实现“治理不失控、执行更敏捷”。
240 1
|
4月前
|
缓存 C++ Python
VS Code中选择Python解释器后,右侧仍显示“Select Python Interpreter”,终端仍为(base)环境。
VS Code中选择Python解释器后,右侧仍显示“Select Python Interpreter”,终端仍为(base)环境。此为环境缓存问题,解决方法:打开命令面板(Ctrl+Shift+P),执行“Python: Clear Cache and Reload Window”,或手动删除Python Environments配置即可恢复正常。
436 0
|
7月前
|
Rust Linux 开发者
再见 pip!Rust 写的 uv 正在把 Python 包管理按在地上摩擦
Python开发者最头疼的依赖管理与环境配置难题,终于有解!uv——Rust编写的超快包管理器,安装、解析、虚拟环境创建速度达pip的10-100倍。支持一键装Python版本、运行脚本、编译依赖,正重塑Python开发工作流。(239字)
547 1
|
6月前
|
人工智能 自然语言处理 供应链
做跨境代购 APP,用好这几个技巧就够了
跨境代购仍有机会,但已从“信息差”转向“系统化运营”。taocarts系统助力效率提升3-5倍、成本降40%-50%,支持自动采购、多语言、实时汇率、物流追踪与私域运营,赋能OPC(一人跨境)新模式。
369 1
|
9月前
|
存储 传感器 人工智能
智耕沃野:AI 的发展将深刻重塑农业格局
随着人工智能技术的迅猛发展,其与农业的融合正从概念探索步入全面推广阶段。本文聚焦 AI 对农业的深层影响,从生产效率革新、资源优化配置、产业模式升级三大维度,结合无人机植保、智能育种、精准灌溉等实践案例,剖析 AI 如何推动农业从 “经验驱动” 向 “数据驱动” 转型。同时探讨技术成本、数据壁垒、人才短缺等现实挑战,并提出针对性推进路径,论证 AI 将成为破解农业发展瓶颈、实现高质高效可持续发展的核心驱动力,其对农业的影响将是全方位、颠覆性的。
|
8月前
|
人工智能 前端开发 小程序
AI开发实战1、手摸手教你一行代码不写,全程AI写个小程序——前端布局
本文揭秘如何用“反向思维”驯服AI写前端:告别千篇一律的赛博朋克渐变风,通过精准Prompt、全局样式锚定与Mock先行策略,让AI稳定输出风格统一、逻辑闭环的整套项目代码。附开源小程序实战案例!
825 0
AI开发实战1、手摸手教你一行代码不写,全程AI写个小程序——前端布局
|
8月前
|
机器学习/深度学习 前端开发 算法
SBTI的爆火
SBTI是MBTI的戏谑变体,借熟为人知的认知框架,用网络热梗命名(如“吗喽”“酒鬼”)替代严肃人格标签。纯前端实现、零后端依赖,结果稳定易传播。它不追求心理科学性,而是精准击中当代青年自嘲、情绪化表达需求,成为社交平台的现象级“状态名片”。以上服务部署全部托管oss
1447 6
|
8月前
|
存储 缓存 搜索推荐
《QClaw备份指南:一次配置终身复用,重装系统再也不用从零开始》
本文针对QClaw用户重装系统后配置与技能全部归零的普遍痛点,深度剖析了其区别于普通软件的分布式文件存储架构,明确划分出模型配置、技能数据、上下文记忆、个人偏好四大核心备份模块。文章基于长期深度使用的实践经验,详细讲解了选择性备份关键文件、技能关联数据同步、跨平台路径适配、增量备份与多位置自动备份的具体方法,同时给出了规范的恢复顺序与避坑要点。全文无冗余理论,所有方法均经过实测验证,可帮助用户实现一次配置终身复用,彻底解决重装系统从头配置的繁琐问题。
732 2
|
10月前
|
人工智能 自然语言处理 供应链
智能客服系统对企业的应用价值:人机协同重构企业服务体系(2026年2月最新)
截至2026年,全球智能客服市场规模达587亿美元(CAGR 24.3%),中国82%企业推行人机协同,金融/电商/电信覆盖率超85%。AI承担68%常规咨询,坐席响应提速42%,客户满意度达85.7分,成本降41%。瓴羊Quick Service以多模态理解、情绪识别、实时协陪等能力,助力企业实现服务即洞察、服务即增长。(239字)