【问题标题】:Spring boot jar not reading/parsing application.properties root folderSpring Boot jar 不读取/解析 application.properties 根文件夹
【发布时间】:2018-05-01 13:11:30
【问题描述】:

情况

我有一个 Spring Boot 应用程序的胖 .jar。我已经使用application.properties 文件将我的配置外部化了。此文件与 .jar 位于同一文件夹中,我从同一文件夹中的命令行启动 .jar(使用命令“java -jar $jarFileName”)。

然后抛出异常:

nested exception is org.springframework.beans.TypeMismatchException: 
Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is 
java.lang.NumberFormatException: For input string: "${elasticsearch.port}"

如您所见,它没有从属性文件中读取值,而是将字符串设置为@Value 注解中的文本,如下所示:

@Value("${elasticsearch.port}")
private int elkPort;

发生这种情况的类用@Component 注释。 根据Spring docs: externalized configuration,spring 应该在 jar 之外读取一个application.properties 文件。

当相同的application.properties 文件放在src/main/resources 中时,它工作正常,因此配置文件似乎正确。

任何想法为什么它不会加载外部配置文件?

编辑 1 我也试过用--spring.config.location=file:application.properties--spring.config.location=file:/full/path/to/application.properties 运行它,但结果和上面一样。

编辑 2:类路径尝试 还尝试了classpath而不是file,与上面的命令相同,但file替换为classpath。 最后尝试不使用任何一个,所以只需--spring.config.location=/path/to/file;再次使用application.properties 的相对路径和完整路径。所有尝试都给出了相同的结果/异常。

编辑 3 我的带注释的应用程序:

@SpringBootApplication
public class ApplicationName {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationName.class, args);
    }
}

编辑 4 尝试添加PropertySourcesPlaceholderConfigurer 如下:

@Configuration
public class PropertyConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

然后我为每个@Value添加了一个默认值;它仍然只解析为默认值而不是application.properties 值。

【问题讨论】:

  • 文件可读吗?它是否具有正确的权限,以便您的应用程序可以实际读取文件。
  • 是的。为了确保这不是错误,我给了每个使用过的读取权限,并以管理员身份运行了 jar。
  • 你的 @SpringBootApplication 注释类是什么样的。您不会尝试使用 @PropertySource 自己加载文件吗?
  • 不,我不是,我已将@SpringBootApplication 课程添加到主帖中。
  • 不要添加PropertySourcesPlaceholderConfigurer Spring Boot 会处理这个问题。如果它不起作用,您的应用程序中的某些内容会禁用常规行为。

标签: java spring-boot configuration application.properties


【解决方案1】:

好吧,经过一番努力,我找到了解决方案。我与PropertySourcesPlaceholderConfigurer 很接近,但还没有到那里;这是现在的完整课程:

@Configuration
public class PropertyConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        final PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();

        ppc.setIgnoreResourceNotFound(true);

        final List<Resource> resources = new ArrayList<>();

        resources.add(new FileSystemResource("relative/path/to/application.properties"));

        ppc.setLocations(resources.toArray(new Resource[]{}));

        return ppc;
    }
}

编辑

为了演示该问题,我创建了一个存储库来显示该问题,请参见此处:https://github.com/Locitao/test-external-properties

【讨论】:

  • 这不应该是必需的,因为 Spring Boot 已经完成了。它不起作用的事实意味着您的类(路径)中必须有一些东西可以禁用常规行为。
  • 我创建了一个准系统存储库,在依赖关系方面尽可能地模仿我自己的项目,以演示问题。我不知道是什么导致了这个问题,但它也出现在演示中,见这里:github.com/Locitao/test-external-properties
  • 你为什么使用shadowjar?您基本上是在使用该插件处理 Spring Boot...您应该使用 Spring Boot 插件。
  • 删除它(以及 jar 的自定义)并使用 Spring Boot 插件构建 jar 的常规方式就像一个魅力。所以基本上删除影子插件和 jar 自定义,只需使用gradle clean build 来获取可执行的 jar。您正试图超越 Spring Boot,并在此过程中破坏它的工作方式。
  • 实际上,当我使用gradle clean buildgradle clean assemble 时,我回到了之前的问题:stackoverflow.com/questions/50040181/…
【解决方案2】:

正如上面提到的页面上所说,您应该指定外部配置位置

    $ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

尝试不使用 file 关键字 --spring.config.location=/full/path/application.properties

【讨论】:

  • 试过了,配置文件的相对路径和完整路径;以及classpathfile;这些的所有组合都给出了相同的错误。
【解决方案3】:

我刚刚从 Eclipse Spring Boot 项目中取出了我的 application.properties,但它失败了。 然后我将文件放在项目根目录下的 cfg 文件夹中,并添加程序参数:

--spring.config.location=cfg/application.properties

它又开始工作了。也许,如果您尝试文件的相对路径(没有前导 /)(没有“文件:”),它将起作用。

【讨论】:

  • 用cfg文件夹试了一下,不幸的是同样的问题仍然存在。
猜你喜欢
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-08
  • 2018-09-15
  • 2019-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多