繁体   English   中英

如何将外部xml文件推送到Spring Boot嵌入式tomcat Continer中

[英]How to push external xml file into spring boot embedded tomcat continer

我已经创建了springboot项目,该项目给了胖子。 我想将运行时的外部xml文件推送到其中。我想将该xml文件放入spring-boot-tomcat容器中。 尝试了许多方法(@import,-spring.config.location等)对我来说不可行。

该xml文件是ApplicationInsight.xml,用于将遥测从我们的应用程序发布到Azure门户。

非常感谢您的帮助。

基于GitHJub问题,我认为问题的一部分是如何传递JVM参数以及如何使用“ spring.config.location”。

我实际上并不熟悉Azure Insights,但是如果我理解正确,它会尝试加载ApplicationInsights.xml文件来进行自我配置,并且它会自动执行此操作。 所以您真的不能像我之前建议的那样在WebConfigurerAdapter中进行设置,因为它在那之前已经进行了初始化,对吗? 无论如何,我都留下了那部分,但是我知道它需要尽快加载,因此我提供了一些其他方法来尽快将文件添加到类路径中。

新的东西

首先看一下您最初在ala GitHub上发布的这一行:

java -jar build/libs/file-gateway.jar --spring.config.location=classpath:/apps/conf/ApplicationInsight.xml 

取而代之的是,该值应该只是一个文件夹路径,而没有“ file”前缀的“ classpath”。 另外,尝试使用“ -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”字符,并从路径中删除了引号。

将文件添加到类路径的其他方法是。

  1. 将目录添加到JVM类路径。

    java -cp“ build / libs / file-gateway.jar:/ apps / conf / *” your.package.MainSpringBootApplication

这要求您指定(通常)用“ @SpringBootApplication”注释的主类,并且包含main方法。 您不会像以前那样执行jar,但仍将其添加到类路径中。

  1. 忘了SpringBoot,然后回到作为JEE开发人员的根源。 在“ src / main / resources / META-INF”文件夹或“ src / main / webapp / META-INF”下为您的应用添加“ context.xml”。 如果要构建可执行的war文件,则我更喜欢后者,而jars则更喜欢前者。

示例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 -->

希望有帮助:)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM