是否可以拥有多个application.properties文件?(编辑:请注意,这个问题演变为标题上的问题.)
我试着有2个文件.
2个文件都命名为'application.properties'.
是否可以"合并"两个文件的内容?(并且第二个属性值覆盖第一个属性值)或者,如果我有一个文件,则忽略另一个文件?
更新1:可以"合并"内容.昨天似乎第一个被忽略了,但似乎是因为当时有些东西被打破了.现在它运作良好.
更新2:它又回来了!同样,只应用了两个文件中的一个.这很奇怪......它是在我使用Spring Tool Suite构建app jar文件之后开始的.似乎Jar版本总是忽略第二个(在类路径中),而在STS上运行的扩展版本的行为会有所不同.我可以从哪里开始调查?
更新3:
Jar版本的行为实际上是正确的.这是java.exe的规范.如果指定了-jar选项,则java.exe将忽略-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>
Run Code Online (Sandbox Code Playgroud)
为此,我引用了以下StackOverflow问题:spring boot属性启动程序无法使用.BTW,在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
Run Code Online (Sandbox Code Playgroud)
请注意,应用程序jar文件包含在loader.path中.
现在加载了C:\ My\External\Dir\config中的application.properties文件.
作为奖励,jar中也可以访问该目录中的任何文件(例如,静态html文件),因为它位于加载器路径中.
对于UPDATE 2中提到的非jar(扩展)版本,可能存在类路径顺序问题.
(顺便说一句,我将问题的标题更改为更具体地解决了这个问题.)
(我研究了类似的问题,但没有一个能解释我在这个问题末尾说明的奇怪行为。)
\n\n我有一个 Spring Boot 1.3.5 应用程序,它坚持用 Boot 的默认图标(绿叶)替换我的图标。为了解决这个问题,我尝试了以下方法:
\n\n街上的传言是,这应该行得通。不幸的是,事实并非如此。
\n\nspring\xe2\x80\x8b.\xe2\x80\x8bmvc\xe2\x80\x8b.\xe2\x80\x8bfavicon\xe2\x80\x8b.\xe2\x80\x8benabled为 false。这应该禁用org\xe2\x80\x8b.\xe2\x80\x8bspringframework\xe2\x80\x8b.\xe2\x80\x8bboot\xe2\x80\x8b.\xe2\x80\x8bautoconfigure\xe2\x80\x8b.\xe2\x80\x8bweb\xe2\x80\x8b.\xe2\x80\x8bWebMvcAutoConfiguration\xe2\x80\x8b.\xe2\x80\x8bWebMvcAutoConfigurationAdapter\xe2\x80\x8b.\xe2\x80\x8bFaviconConfiguration,它似乎负责服务 Boot\ 的默认图标。通过在该类中设置断点,我能够确认当该属性设置为 false 时,该类中定义的 bean 确实不会被创建。
不幸的是,这也没有解决问题。
\n\n实现我自己的图标处理程序:
\n\n@Configuration\npublic class FaviconConfiguration {\n\n @Bean\n public SimpleUrlHandlerMapping faviconHandlerMapping() {\n SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();\n mapping.setOrder(Integer.MIN_VALUE);\n mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));\n return mapping;\n }\n\n @Bean\n protected ResourceHttpRequestHandler faviconRequestHandler() {\n ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();\n ClassPathResource classPathResource = new ClassPathResource("static/");\n List<Resource> locations = Arrays.asList(classPathResource);\n requestHandler.setLocations(locations);\n return requestHandler;\n …Run Code Online (Sandbox Code Playgroud)