简体   繁体   English

传递构造函数参数时未创建Yaml实例

[英]Yaml instance not created when constructor argument is passed

I am using snakeyml to read yml file and load it in Java objects. 我正在使用snakeyml读取yml文件并将其加载到Java对象中。

Issue: When I do a Yaml yml = new Yaml() , yml instance is created. 问题:当我执行Yaml yml = new Yaml() ,会创建yml实例。 But when I pass the constructor argument, the yml instance is not created. 但是,当我传递构造函数参数时,不会创建yml实例。 I don't see an exception either. 我也没有看到例外。 This is the complete code. 这是完整的代码。

private static YamlConfig readStatsConfig()
        throws IOException {

    InputStream input = new FileInputStream(new File(configFile));
    Constructor constructor = new Constructor(YamlConfig.class);

    TypeDescription description = new TypeDescription(YamlConfig.class);
    description.putListPropertyType("resources", YamlConfig.Resource.class);
    constructor.addTypeDescription(description);

    TypeDescription description = new TypeDescription(
                    YamlConfig.Resource.class);
    description.putListPropertyType("stats", YamlConfig.StatsInfo.class);
    constructor.addTypeDescription(description);

    Yaml yaml = new Yaml(constructor);

    YamlConfig cfg = (YamlConfig) yaml.load(input);

    mainLogger.info(cfg);

    return cfg;
}

Code exit in the following statement: 在以下语句中退出代码:

Yaml yaml = new Yaml(constructor);
I fixed the issue by excluding snakeyml dependency in TestNG JARs. All we need to do is to add the following in the project POM has dependency with TestNG.

<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.3.1</version>
             <type>jar</type>
            <exclusions>
                <exclusion>
                    <artifactId>snakeyaml</artifactId>
                    <groupId>org.yaml</groupId>
                </exclusion>
            </exclusions>
        </dependency>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递给超级构造函数的构造函数参数丢失 - Constructor argument gets lost when passed to super constructor DOM元素作为参数传递给构造函数时,抛出NullPointer异常 - DOM Element throws NullPointer Exception when Passed into Constructor as Argument 当一个类的实例本身被创建时,为什么构造函数中的语句没有被执行? - When an instance of a class is created in itself, then why the statements in the constructor are not executed? 如果未在main方法中创建类的实例,是否会调用默认构造函数? - When an instance of a class is not created in the main method, will the default constructor be called? 单元测试:如何验证参数是否传递给构造函数? - Unit testing: how to verify that an argument was passed to a constructor? 当 object 作为另一个 class 的构造函数的参数传递时,它是否引用相同的 object? - Does it refer to the same object or not when an object is passed as an argument of the constructor of another class? 当参数要通过多种方法传递时,是否应该将其保存到实例变量? - Should you save an argument to an instance variable when it's going to be passed through multiple methods? Java - 实例变量依赖于构造函数参数 - Java - Instance variable is dependent on Constructor argument 当构造函数将字符串数组作为参数时,使用反射创建对象的实例 - creating instance of object using reflection , when the constructor takes an array of strings as argument java中如何在不调用构造函数的情况下创建实例 - How instance is created without calling constructor in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM