Spring 从命令行引导读取属性文件无法解析占位符'ConfigPath'
Spring Boot read properties file from command line Could not resolve placeholder 'ConfigPath'
我正在尝试从命令行读取配置文件。主要是我这样做:
public static void main(String[] args) {
if(args.length > 0) {
SpringApplication.run(HeliosAdminBackendApplication.class, args);
} else {
System.out.println("Error");
System.exit(0);
}
}
而且,,我创建了一个 class 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
我的 application.yml 文件和我的 jar 是同一个文件夹。我还将路径设置为 C:/my/path/to/folder/
但错误仍然存在。路径写错了吗?还是我必须 add/modify 代码中的某些内容?
编辑
完整堆栈跟踪:
Exception in thread "main" java.lang.reflect.InvocationTargetException
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: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)
Caused by:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed
to parse configuration class
[it.sysdata.helios_backend_admin.HeliosAdminBackendApplication];
nested exception is java.lang.IllegalArgumentException: Could not
resolve placeholder 'ConfigPath' in value "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.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(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)
at it.sysdata.helios_backend_admin.HeliosAdminBackendApplication.main(HeliosAdminBackendApplication.java:24)
... 8 more Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'ConfigPath' in value
"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 more
以下是您关于为什么我必须使用 "additional" 而不仅仅是位置的问题的答案?
首先,当您使用 spring.config.location 加载配置属性时,spring-boot 会尝试在类路径或配置目录下搜索配置。这是搜索顺序-
file:./config/
file:./
classpath:/config/
classpath:/
但请记住,如果您使用的是 spring.config.locaton,那么它将始终查找 "classpath" 或 "config" 而不是外部配置。
要加载外部 configuration/custom 配置然后 spring 引导提供 "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" 来加载外部配置。
我正在尝试从命令行读取配置文件。主要是我这样做:
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
我的 application.yml 文件和我的 jar 是同一个文件夹。我还将路径设置为 C:/my/path/to/folder/
但错误仍然存在。路径写错了吗?还是我必须 add/modify 代码中的某些内容?
编辑
完整堆栈跟踪:
Exception in thread "main" java.lang.reflect.InvocationTargetException 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: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) Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [it.sysdata.helios_backend_admin.HeliosAdminBackendApplication]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'ConfigPath' in value "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.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(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) at it.sysdata.helios_backend_admin.HeliosAdminBackendApplication.main(HeliosAdminBackendApplication.java:24) ... 8 more Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'ConfigPath' in value "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 more
以下是您关于为什么我必须使用 "additional" 而不仅仅是位置的问题的答案?
首先,当您使用 spring.config.location 加载配置属性时,spring-boot 会尝试在类路径或配置目录下搜索配置。这是搜索顺序-
file:./config/
file:./
classpath:/config/
classpath:/
但请记住,如果您使用的是 spring.config.locaton,那么它将始终查找 "classpath" 或 "config" 而不是外部配置。
要加载外部 configuration/custom 配置然后 spring 引导提供 "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" 来加载外部配置。