Spring boot如何读取jar外的属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41754459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:19:41  来源:igfitidea点击:

Spring boot how to read properties file outside jar

springspring-bootpropertiesconfiguration

提问by Benjamin Liu

in my target folder, there are 2 folders, lib and conf. all the properties files are placed in conf folder, and jars are placed in lib foulder.

在我的目标文件夹中,有 2 个文件夹,lib 和 conf。所有属性文件都放在 conf 文件夹中,jar 放在 lib 文件夹中。

previous to spring boot, we use following configuration in spring.xml to use @value

在 spring boot 之前,我们在 spring.xml 中使用以下配置来使用 @value

<context:property-placeholder location="classpath*:*.properties"/>

and in java code like:

并在java代码中,如:

@Value("${name}")

private String name;

but in spring boot, i don't know how to do the same in java code.

但是在spring boot中,我不知道如何在java代码中做同样的事情。

i have tried following, but not work

我试过跟随,但不起作用

@Configuration
@PropertySource(value = "classpath:aaa.properties")
public class AppConfig {
    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
    }
}

回答by Alberto

I'm a bit confused by the title of the question and the description. Hopefully I won't confuse you even more with my comments.

我对问题的标题和描述感到有些困惑。希望我的评论不会让您更加困惑。

In general, Spring Boot is VERY opiniated about project structure as well as the binary created. The recomended way (Spring Boot opinion) is to build a jar with all dependencies inside (fat jar). If you need configuration properties defined outside your fat jar (or war if that's what you built), Spring Boot offers many options (see reference 1). I like my apps to point to an external file using the flag (spring.config.location) which can be set with a system property:

一般来说,Spring Boot 对项目结构以及创建的二进制文件非常有意见。推荐的方式(Spring Boot 意见)是构建一个包含所有依赖的 jar(fat jar)。如果您需要在您的胖 jar 之外定义配置属性(或者War,如果这是您构建的),Spring Boot 提供了许多选项(请参阅参考资料 1)。我喜欢我的应用程序使用标志 (spring.config.location) 指向外部文件,该标志可以使用系统属性进行设置:

java -jar -Dspring.config.location=<path-to-file> myBootProject.jar

Notice that you can do something similar by using an environment variable to define where your external file lives.

请注意,您可以通过使用环境变量来定义外部文件所在的位置来执行类似的操作。

I hope this helps!

我希望这有帮助!

References: 1. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

参考资料: 1. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

回答by Cristian Caram Pe?alver

I am not sure if you are dealing with the same situation than me, but in my case I have a jar and a *.properties file outside of it. What I did to get the *.properties file located outside the jar was the next:

我不确定您是否正在处理与我相同的情况,但在我的情况下,我有一个 jar 和一个 *.properties 文件。我为获取位于 jar 外的 *.properties 文件所做的工作是下一个:

@Configuration
public class ApplicationContext {

  @Bean
  public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
    properties.setLocation(new FileSystemResource("application.properties"));
    properties.setIgnoreResourceNotFound(false);

    return properties;
  }
}

When I am setting the location of the application.properties file I created FileSystemResource object, which allow me get the properties.files which is located next to the jar. If your .properties files are in classpath, for example, you can use other classes (like ClassPathResource). You can read other classes that spring offers to get a Resource object under the package org.springframework.core.io..

当我设置 application.properties 文件的位置时,我创建了 FileSystemResource 对象,它允许我获取位于 jar 旁边的 properties.files。例如,如果您的.properties 文件在类路径中,您可以使用其他类(如 ClassPathResource)。您可以阅读 spring 提供的其他类来获取 org.springframework.core.io 包下的 Resource 对象。.

I hope this comments helps.

我希望这些评论有帮助。

回答by jayantS

As mentioned in the Spring Boot docs,

正如Spring Boot 文档中提到的,

SpringApplication will load properties from application.propertiesfiles in the following locations and add them to the Spring Environment:

  1. A /config subdirectory of the current directory.
  2. The current directory
  3. A classpath /config package
  4. The classpath root

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

SpringApplication 将从application.properties以下位置的文件加载属性并将它们添加到 Spring Environment:

  1. 当前目录的 /config 子目录。
  2. 当前目录
  3. 一个类路径 /config 包
  4. 类路径根

该列表按优先级排序(在列表中较高位置定义的属性覆盖在较低位置定义的属性)。

One way is to simply rename your 'conf' directory to 'config' and it will work without a problem. So there is no need to do extra configuration until and unless you want your properties file at some location other than the 4 mentioned above.

一种方法是简单地将您的 'conf' 目录重命名为 'config',它可以正常工作。因此,除非您希望属性文件位于上述 4 以外的某个位置,否则无需进行额外配置。

In that case you can define the property source explicitly.

在这种情况下,您可以明确定义属性源。

@PropertySource("classpath:config.properties")

and for multiple properties file

和多个属性文件

@PropertySources({
    @PropertySource("classpath:config.properties"),
    @PropertySource("classpath:logging.properties"),
    @PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)
})

回答by Benjamin Liu

Found an solution:

找到了解决办法:

first create a class and add @ConfigurationProperties

首先创建一个类并添加@ConfigurationProperties

@ConfigurationProperties(prefix = "asdf", locations = "file:conf/aaa.properties")
public class ASDF {
    private String name;   

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Please noted in locations, i use file, not classpath.

请注意位置,我使用文件,而不是类路径。

then in your application class, add @EnableConfigurationProperties

然后在您的应用程序类中,添加 @EnableConfigurationProperties

@SpringBootApplication
@EnableConfigurationProperties({ASDF.class, BBB.class})
public class InitialBeanTestApplication {

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

then you can read config file in the conf folder

然后你可以读取conf文件夹中的配置文件

回答by Benjamin Liu

Found another solution.

找到了另一个解决方案。

put every configuration in one application.properties files, and in code use @Value("${name}") to read.

将每个配置放在一个 application.properties 文件中,并在代码中使用 @Value("${name}") 读取。

and use a assembly file to copy the resource folders' file into target config folder.

并使用程序集文件将资源文件夹的文件复制到目标配置文件夹中。

and after deploy, just need to change application.properties file in config folder and the run the application.

部署后,只需要更改 config 文件夹中的 application.properties 文件并运行应用程序。

this because spring boot read application.properties file in follow sequence.

这是因为 spring boot 按顺序读取 application.properties 文件。

? The /config subdirectory located in the current directory

? 位于当前目录中的 /config 子目录

? The current directory

? 当前目录

? A classpath /config package

? 一个类路径 /config 包

? The classpath root

? 类路径根

but this works for one properties file. not for multiply properties files

但这适用于一个属性文件。不适用于多重属性文件

回答by Saorikido

Actually, the most easiest way is to put application.propertiesand your.jarinto the same directory, and just java -jar your.jarwill automatically load this external config file.

其实最简单的方法就是把application.propertiesyour.jar放到同一个目录下,java -jar your.jar只会自动加载这个外部配置文件。

And if you are trying to load some context/files from resource folder via ClassPathResource, when you read the file don't use pathResource.getFile(), instead use pathResource.getInputStream(), getFile()works well within your IDE, however it doesn't work with the standalone jar.

如果您尝试通过 加载资源文件夹中的某些上下文/文件ClassPathResource,当您读取文件时,请不要使用pathResource.getFile(),而是使用pathResource.getInputStream(),getFile()在您的 IDE 中运行良好,但是它不适用于独立的jar.