YAML 错误开始 API:位置 '???'必须以'*/'结尾

YAML Error starting API: Location '???' must end with '*/'

我正在尝试加载位于 src/main/resources/properties 文件夹中的配置文件。这是我的项目结构:

为了加载这些文件,我使用了以下 VM 参数:

-Dconfig=classpath://properties/xfb.common.properties;classpath://properties/cfb.cm.properties
-Dspring.profiles.active=classpath://properties/xfb.common.properties;classpath://properties/cfb.cm.properties

但是,当我 运行 应用程序时,出现以下错误:

10:54:59.608 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.IllegalStateException: Location 'file:./config/*/application-classpath://properties/xfb.common.properties;classpath://properties/xfb.cm.properties.yaml' must end with '*/'
    at org.springframework.util.Assert.state(Assert.java:97)
    at org.springframework.boot.context.config.LocationResourceLoader.validatePattern(LocationResourceLoader.java:134)
    at org.springframework.boot.context.config.LocationResourceLoader.getResources(LocationResourceLoader.java:95)
    at org.springframework.boot.context.config.StandardConfigDataLocationResolver.resolvePattern(StandardConfigDataLocationResolver.java:311)
    at org.springframework.boot.context.config.StandardConfigDataLocationResolver.resolve(StandardConfigDataLocationResolver.java:297)
    at org.springframework.boot.context.config.StandardConfigDataLocationResolver.resolve(StandardConfigDataLocationResolver.java:249)
    at org.springframework.boot.context.config.StandardConfigDataLocationResolver.resolveProfileSpecific(StandardConfigDataLocationResolver.java:148)
    at org.springframework.boot.context.config.ConfigDataLocationResolvers.lambda$resolve(ConfigDataLocationResolvers.java:120)
    at org.springframework.boot.context.config.ConfigDataLocationResolvers.resolve(ConfigDataLocationResolvers.java:126)
    at org.springframework.boot.context.config.ConfigDataLocationResolvers.resolve(ConfigDataLocationResolvers.java:119)
    at org.springframework.boot.context.config.ConfigDataLocationResolvers.resolve(ConfigDataLocationResolvers.java:107)
    at org.springframework.boot.context.config.ConfigDataImporter.resolve(ConfigDataImporter.java:105)
    at org.springframework.boot.context.config.ConfigDataImporter.resolve(ConfigDataImporter.java:97)
    at org.springframework.boot.context.config.ConfigDataImporter.resolveAndLoad(ConfigDataImporter.java:85)
    at org.springframework.boot.context.config.ConfigDataEnvironmentContributors.withProcessedImports(ConfigDataEnvironmentContributors.java:116)
    at org.springframework.boot.context.config.ConfigDataEnvironment.processWithProfiles(ConfigDataEnvironment.java:311)
    at org.springframework.boot.context.config.ConfigDataEnvironment.processAndApply(ConfigDataEnvironment.java:232)
    at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:102)
    at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:94)
    at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:102)
    at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:87)
    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:85)
    at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared(SpringApplicationRunListeners.java:66)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:344)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:302)
    at api_module.ApiModule.main(ApiModule.java:52)

我使用的是以下版本:

你知道我为什么会收到这个错误吗?

spring.profiles.active VM 参数应包含您要使用的 Spring 配置文件的逗号分隔列表。例如,如果你想使用“dev”和“customprofile2”配置文件,你可以像这样传递它:

-Dspring.profiles.active=dev,customprofile2

然后 Spring 将加载 profile-specific 配置文件,如 the documentation 中所述。 Spring 将查找配置文件的一个位置是在 ./config/ 目录中,例如 ./config/application-{profile}.yaml。在我的例子中是 ./config/application-dev.yml./config/application-customprofile2.yml.

现在,在您的情况下,您使用 spring.profiles.active VM 参数来传递文件列表。通过这样做,Spring 将查找名为 ./config/application-classpath://properties/xfb.common.properties;classpath://properties/xfb.cm.properties.yaml 的文件。由于这不是有效文件,因此会引发错误。

如果要加载这些属性文件,可以使用--spring.config.location参数。例如:

--spring.config.location=classpath:/properties/xfb.common.properties,classpath:/properties/xfb.cm.properties

确保使用逗号分隔文件,并将 classpath:// 替换为 classpath:/

加载外部化配置文件还有多种其他方法。要查看有哪些选项,请查看 the documentation.