我有一个spring boot应用程序,该应用程序从xml中读取一些密码,而这不是jar的一部分。

它必须在单独的文件夹中。如何将这个xml文件添加到类路径中,这样getResourceAsStream("myxml.xml")应该可以工作?

                MyConfig myConfig = null;
    //before
                JAXBContext jaxbContext = JAXBContext.newInstance(MyConfig.class);
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    //This should work
                InputStream is = this.getClass().getClassLoader().getResourceAsStream("myxml.xml");
                myConfig = (MyConfig) jaxbUnmarshaller.unmarshal(is);
    //After

最佳答案

您需要使用PropertiesLauncher。因此,您需要配置Spring boot Maven插件:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <layout>ZIP</layout><!-- enables PropertiesLauncher -->
            </configuration>
        </plugin>
    </plugins>
</build>


然后,您可以使用-Dloader.path=/your/folder/containing/password/file/启动您的应用程序。此外,您需要在路径中使用斜杠调用getResourceAsStream("/myxml.xml")

关于java - 在Spring Boot中将包含密码的外部xml文件添加到类路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46728122/

10-12 00:34
查看更多