阿卡ConfigException $ WrongType:myconf有型字符串,而不是OBJECT

问题描述 投票:2回答:2

我想读的阿卡外部的conf /属性文件。我指这个post

我只有一个配置,我需要在myappConfiguration.properties添加更多的属性。我可以看到属性文件是能够加载,但不能将读取配置。

myappConfiguration.properties内容如下。

myconf {
    directory : "D:\\Git\\Java_Test_WS\\file.reader\\"
}

我越来越波纹管例外,例外情况是清楚的告诉类型的一些问题,但我不明白我应该写。

Exception in thread "main" com.typesafe.config.ConfigException$WrongType: myappConfiguration.properties: myconf has type STRING rather than OBJECT
    at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(SimpleConfig.java:159)
    at com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:145)
    at com.typesafe.config.impl.SimpleConfig.findOrNull(SimpleConfig.java:172)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:184)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:189)
    at com.typesafe.config.impl.SimpleConfig.getObject(SimpleConfig.java:258)
    at com.typesafe.config.impl.SimpleConfig.getConfig(SimpleConfig.java:264)
    at com.typesafe.config.impl.SimpleConfig.getConfig(SimpleConfig.java:37)
    at com.myapp.file.reader.AkkaBaseEventReader.main(AkkaBaseEventReader.java:27)

下面是我想读的是配置项的Java代码。而我得到异常的行号27码:

Config config = system.settings().config().getConfig("myconf.directory");

完整的代码:

public class AkkaBaseEventReader implements ApplicationConstant {

    public static void main(String[] args) throws IOException {

        System.setProperty("config.file","myappConfiguration.properties");

        ActorSystem system = ActorSystemImpl.create("FileReaderForassignment");
        final ActorRef reader = system.actorOf(EventScannerActor.props(),"FileReader");

        System.out.println(system.settings());

        Config config = system.settings().config().getConfig("myconf.directory");       

    }
}

下面是我的pom.xml,会告诉我使用的是什么版本阿卡:

<properties>
        <akka.version>2.4.9</akka.version>
        <maven-dependency-plugin.version>3.0.0</maven-dependency-plugin.version>
        <maven.compiler.plugin>3.6.1</maven.compiler.plugin>
        <java.compiler.target>1.8</java.compiler.target>
        <java.compiler.source>1.8</java.compiler.source>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.11</artifactId>
            <version>${akka.version}</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-http-core_2.11</artifactId>
            <version>${akka.version}</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-http-experimental_2.11</artifactId>
            <version>${akka.version}</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-http-jackson-experimental_2.11</artifactId>
            <version>${akka.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>${maven-dependency-plugin.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!-- This will download source so easy to see API and java doc. -->
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Java 8 compiler plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin}</version>
                <configuration>
                    <source>${java.compiler.source}</source>
                    <target>${java.compiler.target}</target>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

预先感谢任何形式的帮助和信息。

java akka configuration-files application-settings
2个回答
2
投票

根据您的配置目录中的值是一个String而不是另一个Config对象:

myconf {
  directory : "D:\\Git\\Java_Test_WS\\file.reader\\" 
}

为了得到值,你应该做一个String

Config config = system.settings().config().getString("myconf.directory");

1
投票

我也有类似的问题,其原因是使用了错误的配额,同时设置在Java参数属性。

我错了Java参数

-Dlocal=true

我的代码

    Config config = ConfigFactory.load(Thread.currentThread().getContextClassLoader());
    config = config.getConfig("local"); //exception thrown here

引发的异常是

Caused by: com.typesafe.config.ConfigException$WrongType: system properties: local has type STRING rather than OBJECT
at com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:133)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:145)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:159)

的解决方案是使用单个配额

-Dlocal='true'

这与“com.typesafe.config”版本发生:1.2.1

<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.2.1</version>
© www.soinside.com 2019 - 2024. All rights reserved.