简体   繁体   English

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

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

i have created springboot project which gives fat-jar. 我已经创建了springboot项目,该项目给了胖子。 i want to push external xml file in runtime into it.i want to place that xml file into spring-boot-tomcat container. 我想将运行时的外部xml文件推送到其中。我想将该xml文件放入spring-boot-tomcat容器中。 tried many ways to do it (@import, --spring.config.location,etc) those ways didn't work out for me. 尝试了许多方法(@import,-spring.config.location等)对我来说不可行。

That xml file is ApplicationInsight.xml, which is used to post telemetry from our application to Azure portal. 该xml文件是ApplicationInsight.xml,用于将遥测从我们的应用程序发布到Azure门户。

Highly appreciate any help. 非常感谢您的帮助。

Based on the GitHJub issue, I think part of the problem is how you are passing JVM parameters, and how you are using "spring.config.location". 基于GitHJub问题,我认为问题的一部分是如何传递JVM参数以及如何使用“ spring.config.location”。

I am not familiar with Azure Insights really, but if I understand correctly, it is trying to load the ApplicationInsights.xml file to configure itself, and it's doing this automatically. 我实际上并不熟悉Azure Insights,但是如果我理解正确,它会尝试加载ApplicationInsights.xml文件来进行自我配置,并且它会自动执行此操作。 So you really can't set it up in the WebConfigurerAdapter as I previously suggested because it has already initialized itself before that, correct? 所以您真的不能像我之前建议的那样在WebConfigurerAdapter中进行设置,因为它在那之前已经进行了初始化,对吗? I left that part in anyways, but I get that it needs to be loaded sooner so I provided a few additional ways to add the file to the classpath ASAP. 无论如何,我都留下了那部分,但是我知道它需要尽快加载,因此我提供了一些其他方法来尽快将文件添加到类路径中。

New Stuff 新的东西

First take a look at this line you had originally posted ala GitHub: 首先看一下您最初在ala GitHub上发布的这一行:

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

Instead the value should be just a folder path, without "classpath" of "file" prefix. 取而代之的是,该值应该只是一个文件夹路径,而没有“ file”前缀的“ classpath”。 Also, try using '-D' instead of '--'. 另外,尝试使用“ -D”代替“-”。

java -jar build/libs/file-gateway.jar -Dspring.config.location=/apps/conf/

The property is supposed to either refer to a directory containing auto configuration property files for Spring Boot. 该属性应该引用包含Spring Boot自动配置属性文件的目录。 It can also work for referring to a specific "application.properties|yml" file. 它也可以用于引用特定的“ application.properties | yml”文件。

With that, my previous suggestion may work for you. 这样,我以前的建议可能对您有用。

Old Suggestion 旧建议

If you require a unique way for loading resources, you can add a resource handler to your application. 如果您需要一种独特的方式来加载资源,则可以向应用程序中添加资源处理程序。

@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Value("${telemetry.folder}")
    private String telemetryFolder;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
          .addResourceLocations(telemetryFolder); 
    }
}

And/or you could load it with apache IO: 和/或您可以使用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());
}

But this will only work if the api you are using doesn't need to be initialized much earlier. 但这仅在您使用的api不需要更早初始化的情况下才有效。

More New Stuff 更多新东西

In your last post on the GitHub issue, you tried this: 在GitHub问题的上一篇文章中,您尝试了以下操作:

java -jar build/libs/file-gateway.jar -applicationinsights.configurationDirectory="/apps/conf/"

Instead, try adding the property as a jvm parameter like this: 相反,请尝试将属性添加为jvm参数,如下所示:

java -jar build/libs/file-gateway.jar -Dapplicationinsights.configurationDirectory=/apps/conf/

Notice that I added a capital 'D' character after the, and I removed the quotes from the path. 请注意,我在的后面添加了大写的“ D”字符,并从路径中删除了引号。

Other ways to add the file to classpath are. 将文件添加到类路径的其他方法是。

  1. Add the directory to the JVM classpath. 将目录添加到JVM类路径。

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

This requires that you specify the main class which is (commonly) annotated with '@SpringBootApplication' and contains the main method. 这要求您指定(通常)用“ @SpringBootApplication”注释的主类,并且包含main方法。 You do not execute the jar like before, but you do still add it to the classpath. 您不会像以前那样执行jar,但仍将其添加到类路径中。

  1. Forget about SpringBoot, and go back to your roots as a JEE developer. 忘了SpringBoot,然后回到作为JEE开发人员的根源。 Add a "context.xml" for your app under the "src/main/resources/META-INF" folder, or "src/main/webapp/META-INF". 在“ src / main / resources / META-INF”文件夹或“ src / main / webapp / META-INF”下为您的应用添加“ context.xml”。 I prefer the later if I'm building an executable war file, and the former for jars. 如果要构建可执行的war文件,则我更喜欢后者,而jars则更喜欢前者。

Example context.xml: 示例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>

You can also use JVM parameters with EL. 您还可以将JVM参数与EL一起使用。

So if you execute the jar with this: 因此,如果您使用以下命令执行jar:

java -jar build/libs/file-gateway.jar -Dapplicationinsights.configurationDirectory=/apps/conf/

You could set the resources base with this: 您可以使用以下方法设置资源库:

<!--snip -->
<PreResources base="${applicationinsights.configurationDirectory}" 
<!--snip -->

Hope that helps:) 希望有帮助:)

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

相关问题 spring boot的嵌入式tomcat服务器如何加载外部“war”文件 - How load a external "war" file to the embedded tomcat server of spring boot 如何在Spring Boot Embedded tomcat中维护外部静态HTML文件? - How to service external static HTML files in Spring Boot Embedded tomcat? Spring 引导微服务嵌入式 tomcat 与外部 tomcat - Spring Boot microservices Embedded tomcat vs External tomcat 在 spring-boot 中的嵌入式 apache tomcat 中配置“tomcat-server.xml”文件 - Configuring 'tomcat-server.xml' file inside of embedded apache tomcat in spring-boot 如何在运行时的Spring Boot中重新加载嵌入式Tomcat? - How to reload embedded Tomcat in Spring Boot in runtime? 在春季启动时,是否可以在嵌入式/内置的Tomcat服务器中部署外部war文件? - Is it possible to deploy a external war file in embedded/inbuilt tomcat server in spring boot? Spring Boot、Debian、嵌入式 Tomcat 关闭外部端口 - Spring Boot, Debian, Embedded Tomcat close external port 如何在运行时使用Spring Boot App中嵌入的Tomcat加载外部War文件 - How Can I Load External War Files at Runtime with Tomcat Embedded in Spring Boot App spring boot tomcat外部属性文件 - spring boot tomcat external properties file Spring 引导外部 Tomcat 文件上传 - Spring Boot External Tomcat File Upload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM