基于 GitHJub 问题,我认为部分问题在于您如何传递 JVM 参数,以及您如何使用“spring.config.location”。
我真的不熟悉 Azure Insights,但如果我理解正确,它会尝试加载 ApplicationInsights.xml 文件来配置自身,并且它会自动执行此操作。所以你真的不能像我之前建议的那样在 WebConfigurerAdapter 中设置它,因为它在此之前已经初始化了自己,对吗?无论如何我都留下了那部分,但我知道它需要尽快加载,所以我提供了一些额外的方法来尽快将文件添加到类路径中。
新东西
首先看看你最初在 GitHub 上发布的这一行:
java -jar build/libs/file-gateway.jar --spring.config.location=classpath:/apps/conf/ApplicationInsight.xml
相反,该值应该只是一个文件夹路径,没有“文件”前缀的“类路径”。另外,请尝试使用“-D”而不是“--”。
java -jar build/libs/file-gateway.jar -Dspring.config.location=/apps/conf/
该属性应该引用包含 Spring Boot 的自动配置属性文件的目录。它也可以用于引用特定的“application.properties|yml”文件。
这样,我之前的建议可能对你有用。
旧建议
如果您需要一种独特的方式来加载资源,您可以将资源处理程序添加到您的应用程序中。
@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
@Value("${telemetry.folder}")
private String telemetryFolder;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceLocations(telemetryFolder);
}
}
并且/或者您可以使用 apache IO 加载它:
@Value("${telemetry.file}")
private String telemetryFile;
@Autowired
private ResourceLoader resourceLoader;
public String telemtryXml(){
return org.apache.commons.io.IOUtils.toString(resourceLoader.getResource(telemtryFile).getInputStream());
}
但这只有在您使用的 api 不需要更早初始化时才有效。
更多新内容
在您上一篇关于 GitHub 问题的帖子中,您尝试了以下方法:
java -jar build/libs/file-gateway.jar -applicationinsights.configurationDirectory="/apps/conf/"
相反,尝试将属性添加为 jvm 参数,如下所示:
java -jar build/libs/file-gateway.jar -Dapplicationinsights.configurationDirectory=/apps/conf/
请注意,我在 之后添加了一个大写的“D”字符,并从路径中删除了引号。
将文件添加到类路径的其他方法是。
-
将目录添加到 JVM 类路径。
java -cp "build/libs/file-gateway.jar:/apps/conf/*" your.package.MainSpringBootApplication
这要求您指定(通常)用“@SpringBootApplication”注释并包含主方法的主类。您没有像以前那样执行 jar,但仍将其添加到类路径中。
- 忘掉 SpringBoot,回到你作为 JEE 开发人员的本源。在“src/main/resources/META-INF”文件夹或“src/main/webapp/META-INF”下为您的应用添加“context.xml”。如果我要构建可执行的 war 文件,我更喜欢后者,而前者用于 jar。
示例 context.xml:
<?xml version='1.0' encoding='utf-8'?>
<!-- path should be the context-path of you application.
<Context path="/">
<Resources className="org.apache.catalina.webresources.StandardRoot">
<PreResources base="/apps/conf"
className="org.apache.catalina.webresources.DirResourceSet"
internalPath="/"
webAppMount="/WEB-INF/classes"/>
</Resources>
</Context>
您还可以将 JVM 参数与 EL 一起使用。
所以如果你用这个来执行jar:
java -jar build/libs/file-gateway.jar -Dapplicationinsights.configurationDirectory=/apps/conf/
你可以这样设置资源库:
<!--snip -->
<PreResources base="${applicationinsights.configurationDirectory}"
<!--snip -->
希望对您有所帮助:)