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

时间:2019-05-22 10:33:22

标签: java spring-boot jar configuration-files

我正在尝试从命令行读取配置文件。我主要是这样做的:

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

然后following this question,我创建了一个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/,但错误仍然存​​在。路径写错了吗?还是我必须在代码中添加/修改某些内容?

编辑

完整堆栈跟踪:

  

线程“主”中的异常java.lang.reflect.InvocationTargetException           在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机   方法)           在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)           在java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)           在java.base / java.lang.reflect.Method.invoke(Method.java:566)           在org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)           在org.springframework.boot.loader.Launcher.launch(Launcher.java:87)           在org.springframework.boot.loader.Launcher.launch(Launcher.java:50)           在org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)   造成原因:   org.springframework.beans.factory.BeanDefinitionStoreException:失败   解析配置类   [it.sysdata.helios_backend_admin.HeliosAdminBackendApplication];   嵌套异常是java.lang.IllegalArgumentException:无法   在值“ file:$ {ConfigPath}”中解析占位符“ ConfigPath”           在org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:181)           在org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315)处           在org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)处           在org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)           在org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)           在org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:691)处           在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:528)           在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)           在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)           在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)           在org.springframework.boot.SpringApplication.run(SpringApplication.java:316)           在org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)           在org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)           在it.sysdata.helios_backend_admin.HeliosAdminBackendApplication.main(HeliosAdminBackendApplication.java:24)           ... 8更多原因:java.lang.IllegalArgumentException:无法解析占位符'ConfigPath'的值   “文件:$ {ConfigPath}”           在org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172)           在org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)           在org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:237)           在org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:211)           在org.springframework.core.env.AbstractEnvironment.resolveRequired占位符处(AbstractEnvironment.java:575)           在org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:450)           在org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:271)           在org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242)           在org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:191)           在org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:295)           在org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242)           在org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:199)           在org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167)           ...还有21

1 个答案:

答案 0 :(得分:1)

这是您有关为什么我必须使用“附加”而不仅仅是位置的问题的答案?

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

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

但是请记住,如果您使用spring.config.locaton,它将始终查找“ classpath”或“ config”而不是外部配置。

要加载外部配置/自定义配置,则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”来加载外部配置。