【问题标题】:Springboot placed application yaml files in resources/config/ folder, while running the JAR it is not able to find the resourcesSpringboot将应用程序yaml文件放在resources/config/文件夹中,运行JAR时找不到资源
【发布时间】:2021-03-30 09:18:34
【问题描述】:

我的项目结构

AssemblyProject
 |-src/main/java
 |-src/main/resources
   |-config
     |-application-prod.yml
     |-application.yml
     |-application-stage.yml
 |-build.gradle

我能够生成 JAR 文件。 并尝试从 JAR 文件位置执行以下命令

java -jar assembly-services-1.0.0.jar --spring.config.location=classpath:config/ --spring.profiles.active=stage

还有提取的 JAR

我正在低于错误

14:43:15.924 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property 'spring.profiles.active' imported from location 'class path resource [config/application-stage.yaml]' is invalid in a profile specific resource [origin: class path resource [config/application-stage.yaml] from assembly-services-1.0.0.jar - 38:13]
        at org.springframework.boot.context.config.InvalidConfigDataPropertyException.lambda$throwOrWarn$1(InvalidConfigDataPropertyException.java:124)
        at java.base/java.lang.Iterable.forEach(Iterable.java:75)
        at java.base/java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1085)
        at org.springframework.boot.context.config.InvalidConfigDataPropertyException.throwOrWarn(InvalidConfigDataPropertyException.java:121)
        at org.springframework.boot.context.config.ConfigDataEnvironment.checkForInvalidProperties(ConfigDataEnvironment.java:354)
        at org.springframework.boot.context.config.ConfigDataEnvironment.applyToEnvironment(ConfigDataEnvironment.java:323)
        at org.springframework.boot.context.config.ConfigDataEnvironment.processAndApply(ConfigDataEnvironment.java:236)
        at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:97)
        at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:89)
        at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:100)
        at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:86)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
        at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:82)
        at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:63)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
        at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)
        at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111)
        at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:62)
        at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:362)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
        at com.assembly.Application.main(Application.java:73)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:107)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)

注意:我知道这是一个重复的问题,但我确实已经完成了所有过去与此主题相关的 stackoverflow 问题,但遗憾的是没有运气!

【问题讨论】:

  • 为什么是config目录下的文件?
  • 只有客户要求的结构。需要将单独的配置文件保存在单独的分层文件夹中。
  • application-stage.yml 在哪里?我在你的项目结构上看不到它。
  • 您在application-*.yaml 文件中定义了属性spring.profiles.active,不是吗?如果这样做,则需要将其删除。这就是您收到此错误的原因
  • 我相信它与这个属性有关,您可以在单个 application.properties/yaml 中定义所有配置文件,或者您有单独的文件。在你的情况下,它有点混合,ApplicationContext 只是中断。

标签: java spring-boot gradle


【解决方案1】:

我可以看到的问题是您必须在您的application-stage.properties 中也提供了有效的个人资料。你需要删除它。根据 Spring Boot 文档:

因此,在 Spring Boot 2.4 中,我们计划进行两项重大更改 属性和 YAML 文件的加载方式:

文档将按照定义的顺序加载。

无法再从个人资料特定文档激活个人资料。

如果您仍想使用旧版配置,您需要将spring.config.use-legacy-processing=true 提供给您的application.properties

【讨论】:

  • 我已经删除了活动 .profile 的空白属性,这对我有用。如果你能告诉我这是否会在运行时参数和硬编码参数之间造成冲突?
  • 不冲突,但配置文件属性(即spring.profiles.active)不是配置属性的一部分,并且spring boot不再根据新版本的spring boot重新识别它。
  • 谢谢@hiren
  • @DnyaneshwarJadhav 你可以接受答案:)
  • 接受@hiren,:)
猜你喜欢
  • 2016-07-14
  • 1970-01-01
  • 1970-01-01
  • 2022-08-21
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多