Spring 引导从命令行读取属性文件 无法解析占位符'ConfigPath'



>我正在尝试从命令行读取配置文件。总的来说,我这样做:

public static void main(String[] args) {
if(args.length > 0) {
SpringApplication.run(HeliosAdminBackendApplication.class, args);
} else {
System.out.println("Error");
System.exit(0); 
}       
}

而且,在这个问题之后,我创建了一个这样的类MyConfig

import lombok.Getter;
@Configuration
@ConfigurationProperties
@PropertySource(value = "file:${ConfigPath}")
public class MyConfig {
@Getter
@Value("${property_name}")
private String myproperty;
}

然后我创建了.jar文件,然后进入包含 jar 的文件夹并尝试通过以下方式运行它:

java -jar myapp.jar --spring.config.location=file:application.yml

我的应用程序.yml文件与我的jar相同。我还将路径设置为C:/my/path/to/folder/但错误仍然存在。路径写错了吗?还是我必须在代码中添加/修改某些内容?

编辑

全栈跟踪:

线程"main"中的异常 java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native 方法) 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:48) at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) 引起: org.springframework.beans.factory.BeanDefinitionStoreException: Failed 分析配置类 [it.sysdata.helios_backend_admin.HeliosAdminBackendApplication]; 嵌套异常是java.lang.IllegalArgumentException:不能 解析值"file:${ConfigPath}"中的占位符"配置路径" at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:181) at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315) at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessor(PostProcessorRegistrationDelegate.java:275) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessor(PostProcessorRegistrationDelegate.java:95) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessor(AbstractApplicationContext.java:691) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:528) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) 在it.sysdata.helios_backend_admin。HeliosAdminBackendApplication.main(HeliosAdminBackendApplication.java:24) ...8 更多 原因: java.lang.IllegalArgumentException: 无法解析值中的占位符"ConfigPath" "file:${ConfigPath}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172) at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:237) at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:211) at org.springframework.core.env.AbstractEnvironment.resolveRequiredPlaceholders(AbstractEnvironment.java:575) at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:450) at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:271) at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:191) at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:295) at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:199) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ...21 更多

这是您关于为什么我必须使用"额外"而不仅仅是位置的问题的答案?

首先,当你使用 spring.config.location 来加载配置属性时,spring-boot 会尝试在类路径或配置目录下搜索配置。这是搜索的顺序 -

file:./config/
file:./
classpath:/config/
classpath:/

但请记住,如果您使用的是spring.config.locaton,那么它将始终查找"类路径"或"配置"而不是外部配置。

要加载外部配置/自定义配置,Spring boot 提供了"spring.config.additional-location",它按以下顺序搜索配置 -

file:./custom-config/
classpath:custom-config/ (This is was your case)
file:./config/
file:./
classpath:/config/
classpath:/

我希望现在知道为什么使用"spring.config.additional-location"来加载外部配置的答案。

最新更新