Spring Boot:是否可以在带有胖子的任意目录中使用外部application.properties文件?

zeodtr:

是否可以有多个application.properties文件?编辑:请注意,此问题演变为标题上的问题。)

我尝试有2个文件。

  • 第一个位于应用程序Jar的根文件夹中。
  • 第二个在类路径中指定的目录上。

2个文件都命名为“ application.properties”。

是否可以“合并”两个文件的内容?(第二个属性的值覆盖第一个属性的值)或者,如果我有一个文件,那么另一个文件将被忽略?

更新1:可以“合并”内容。昨天似乎第一个被忽略了,但这似乎是因为那时有些问题。现在它运作良好。

更新2:又回来了!同样,仅应用两个文件之一。这很奇怪...它是在我使用Spring Tool Suite构建了app jar文件之后开始的。而且,Jar版本似乎总是忽略第二个版本(在类路径中),而在STS上运行的扩展版本的行为却有所不同。我可以从哪里开始调查?

更新3

Jar版本的行为实际上是正确的。这是java.exe的规范。当指定-jar选项时,java.exe将同时忽略-classpath选项和CLASSPATH环境变量,并且classpath将仅包含jar文件。因此,将忽略类路径上的第二个application.properties文件。

现在,如何加载类路径上的第二个application.properties?

更新4

使用-jar选项时,我设法在外部路径中加载了application.properties文件。

关键是PropertiesLauncher。

要使用PropertiesLauncher,必须按以下方式更改pom.xml文件:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>  <!-- added -->
                <layout>ZIP</layout> <!-- to use PropertiesLaunchar -->
            </configuration>
        </plugin>
    </plugins>
</build>

为此,我引用了以下StackOverflow问题:Spring Boot属性启动器无法使用顺便说一句,在Spring Boot Maven插件文档(http://docs.spring.io/spring-boot/docs/1.1.7.RELEASE/maven-plugin/repackage-mojo.html)中,没有提到指定ZIP触发器使用PropertiesLauncher。(也许在另一个文档中?)

构建jar文件后,通过检查jar中META-INF / MENIFEST.MF中的Main-Class属性,可以看到使用了PropertiesLauncher。

现在,我可以按以下方式运行jar(在Windows中):

java -Dloader.path=file:///C:/My/External/Dir,MyApp-0.0.1-SNAPSHOT.jar -jar MyApp-0.0.1-SNAPSHOT.jar

请注意,应用程序jar文件包含在loader.path中。

现在,已加载C:\ My \ External \ Dir \ config中的application.properties文件。

另外,Jar也可以访问该目录中的任何文件(例如,静态html文件),因为它位于加载程序路径中。

至于UPDATE 2中提到的非jar(扩展)版本,可能存在类路径顺序问题。

(顺便说一句,我更改了该问题的标题,使其更加针对此问题。)

zeodtr:

使用-jar选项时,我设法在外部路径中加载了application.properties文件。

关键是PropertiesLauncher。

要使用PropertiesLauncher,必须按以下方式更改pom.xml文件:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>  <!-- added -->
                <layout>ZIP</layout> <!-- to use PropertiesLaunchar -->
            </configuration>
        </plugin>
    </plugins>
</build>

为此,我引用了以下StackOverflow问题:Spring Boot属性启动器无法使用顺便说一句,在Spring Boot Maven插件文档(http://docs.spring.io/spring-boot/docs/1.1.7.RELEASE/maven-plugin/repackage-mojo.html)中,没有提到指定ZIP触发器使用PropertiesLauncher。(也许在另一个文档中?)

构建jar文件后,通过检查jar中META-INF / MENIFEST.MF中的Main-Class属性,可以看到使用了PropertiesLauncher。

现在,我可以按以下方式运行jar(在Windows中):

java -Dloader.path=file:///C:/My/External/Dir,MyApp-0.0.1-SNAPSHOT.jar -jar MyApp-0.0.1-SNAPSHOT.jar

请注意,应用程序jar文件包含在loader.path中。

现在,已加载C:\ My \ External \ Dir \ config中的application.properties文件。

另外,Jar也可以访问该目录中的任何文件(例如,静态html文件),因为它位于加载程序路径中。

至于UPDATE 2中提到的非jar(扩展)版本,可能存在类路径顺序问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Heroku Spring application.properties文件

在Spring application.properties文件中使用表达式

我可以在Spring Boot应用程序中使用xml属性文件而不是application.properties吗?

有什么方法可以在application.properties文件中写入JSON数组列表(spring-boot)

Spring Boot的外部log4.properties文件

运行测试时如何指示Spring Boot在测试资源中使用application.properties文件

是否可以在每个jar文件中使用带有附加信息的spring配置XML文件?

Spring Boot jar中的application.properties文件在哪里?

从 spring boot application.properties 文件访问 amqp_URI

在Spring XML配置文件中使用Properties String

如何将Spring Boot中的application.properties文件外部化为例如外部依赖的JAR?

当在application.properties中使用spring.profile.default时,Spring Boot不加载默认配置文件

在Spring Boot中将application.properties文件加载到java.util.Properties

如何在Spring Boot中的application.properties中指定外部属性文件?

如何将Spring Boot中的URL外部化为application.properties文件?

如何将Spring Boot application.properties外部化为tomcat / lib文件夹

如何在Spring Boot项目中模拟.properties文件

Spring Boot未加载bootstrap.properties文件

Spring application.properties文件中的布尔值?

Spring Redis-从application.properties文件读取配置

在Spring Boot中将spring.application.json用于application.properties文件

為什麼我無法在 Spring Boot 中使用 application.properties 文件中的屬性?

具有application.properties的spring-boot数据源配置文件

如何在Spring Boot应用程序中使用配置(properties / yml)文件中的属性?

无法使用带有Spring Boot,JUnit5和Kotlin的@TestPropertySource加载test-properties.yaml文件

使用Spring Boot在运行时以编程方式更改application.properties文件

使用特定的application.properties文件将Spring Boot应用程序部署到Heroku

Spring Boot 2:如何使用application.properties文件配置HikariCP

Spring Boot:如何使用UTF-8加载application.properties文件