我有一个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
答案 0 :(得分:2)
您需要使用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")
。