I'm trying to read a configuration file from command line. In main I do this:
public static void main(String[] args) {
if(args.length > 0) {
SpringApplication.run(HeliosAdminBackendApplication.class, args);
} else {
System.out.println("Error");
System.exit(0);
}
}
And, following this question, I've created a class MyConfig
like this:
import lombok.Getter;
@Configuration
@ConfigurationProperties
@PropertySource(value = "file:${ConfigPath}")
public class MyConfig {
@Getter
@Value("${property_name}")
private String myproperty;
}
Then I've created the .jar file, then went in the folder containing the jar and try to run it by doing:
java -jar myapp.jar --spring.config.location=file:application.yml
My application.yml file is the same folder of my jar. I've also set the path to C:/my/path/to/folder/
but the error persists. Is the path written wrong? Or do I have to add/modify something in the code?
EDIT
Full stack trace:
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
Here is the answer for your question regarding Why do I have to use "additional" and not just location?
First of all when ever you use spring.config.location for loading configuration properties then spring-boot trys to search the configuration at classpath or under config directory. Here is the order of search -
file:./config/
file:./
classpath:/config/
classpath:/
But remember if you are using the spring.config.locaton then it will always look for either "classpath" or "config" not the external configuration.
To load the external configuration/custom configuration then spring boot provides "spring.config.additional-location" which search the configuration in following order -
file:./custom-config/
classpath:custom-config/ (This is was your case)
file:./config/
file:./
classpath:/config/
classpath:/
I hope now got the answer why to use the "spring.config.additional-location" for loading external configuration.