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

标签 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/ 但错误仍然存​​在。是不是路径写错了?或者我必须在代码中添加/修改某些内容吗?

编辑

完整堆栈跟踪:

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

最佳答案

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

首先,当您使用 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”来加载外部配置的答案。

关于java - Spring Boot从命令行读取属性文件无法解析占位符 'ConfigPath',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56254834/

相关文章:

java - 正则表达式匹配带或不带千位分隔符java的货币

java - JAX-WS 异常 - "return is not a valid property"

spring - 如何使用 Spring Boot 1.5.2 注入(inject) Jersey 端点接口(interface)的实现?

jar - 在Jar文件中搜索

java - 使用 FatJar 作为 native 库

Java 已安装,在 list 中,但执行产生 "./java: No such file or directory"

java - system.nanoTime() 运行太慢

java - 尝试运行 Spring boot 应用程序时出错。使用名称创建 bean 时出错

java - 尽管允许,但未经授权的请求

java - 通过 C++ 程序修改 .JAR 文件?