Spring Properties File Outside jar – 在jar之外的Spring属性文件

最后修改: 2019年 3月 14日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Property files are a common method that we can use to store project-specific information. Ideally, we should keep it external to the packaging to be able to make changes to the configuration as needed.

属性文件是一种常见的方法,我们可以用它来存储项目的特定信息。理想情况下,我们应该把它保持在包装的外部,以便能够根据需要对配置进行修改。

In this quick tutorial, we’ll look into various ways to load the properties file from a location outside the jar in a Spring Boot application.

在这个快速教程中,我们将研究从Spring Boot应用程序中的jar之外的位置加载属性文件的各种方法。

2. Using the Default Location

2.使用默认位置

By convention, Spring Boot looks for an externalized configuration file — application.properties or application.yml in four predetermined locations in the following order of precedence:

根据惯例。Spring Boot会寻找一个外部化的配置文件 – application属性or application.yml在四个预先确定的位置,其优先级如下。

  • A /config subdirectory of the current directory
  • The current directory
  • A classpath /config package
  • The classpath root

Therefore, a property defined in application.properties and placed in the /config subdirectory of the current directory will be loaded. This will also override properties in other locations in case of a collision.

因此,application.properties中定义并放置在当前目录的/config子目录中的属性将被加载。这也将在发生碰撞的情况下覆盖其他位置的属性。

3. Using the Command Line

3.使用命令行

If the above convention doesn’t work for us, we can configure the location directly in the command line:

如果上述约定对我们不适用,我们可以直接在命令行中配置位置

java -jar app.jar --spring.config.location=file:///Users/home/config/jdbc.properties

We can also pass a folder location where the application will search for the file:

我们还可以传递一个文件夹位置,应用程序将在那里搜索文件。

java -jar app.jar --spring.config.name=application,jdbc --spring.config.location=file:///Users/home/config

Finally, an alternative approach is running the Spring Boot application through the Maven plugin.

最后,另一种方法是通过Maven插件运行Spring Boot应用程序。

There, we can use a -D parameter:

在那里,我们可以使用一个-D参数。

mvn spring-boot:run -Dspring.config.location="file:///Users/home/jdbc.properties"

4. Using Environment Variables

4.使用环境变量

Now let’s say that we can’t alter the startup command.

现在我们说,我们不能改变启动命令。

What’s great is that Spring Boot will also read the environment variables SPRING_CONFIG_NAME and SPRING_CONFIG_LOCATION:

最棒的是,Spring Boot还将读取环境变量SPRING_CONFIG_NAMESPRING_CONFIG_LOCATION

export SPRING_CONFIG_NAME=application,jdbc
export SPRING_CONFIG_LOCATION=file:///Users/home/config
java -jar app.jar

Note that the default file will still be loaded. But the environment-specific property file takes precedence in case of a property collision.

请注意,默认文件仍将被加载。但是环境特定的属性文件优先于在属性碰撞的情况下。

5. Using Application Properties

5.使用应用程序属性

As we can see, we have to define spring.config.name and spring.config.location properties before the application starts, so using them in the application.properties file (or the YAML counterpart) will have no effect.

正如我们所看到的,我们必须在应用程序启动前定义spring.config.namespring.config.location属性,所以在application.properties文件(或YAML对应文件)中使用它们将没有任何效果。

Spring Boot modified how properties are handled in version 2.4.0.

Spring Boot在2.4.0版本中修改了属性的处理方式。

Together with this change, the team introduced a new property that allows importing additional configuration files directly from the application properties:

与这一变化一起,团队引入了一个新的属性,允许直接从应用程序属性中导入额外的配置文件。

spring.config.import=file:./additional.properties,optional:file:/Users/home/config/jdbc.properties

6. Programmatically

6.按程序进行

If we want programmatic access, we can register a PropertySourcesPlaceholderConfigurer bean:

如果我们想要编程访问,我们可以注册一个PropertySourcesPlaceholderConfigurer bean。

public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer properties = 
      new PropertySourcesPlaceholderConfigurer();
    properties.setLocation(new FileSystemResource("/Users/home/conf.properties"));
    properties.setIgnoreResourceNotFound(false);
    return properties;
}

Here we’ve used PropertySourcesPlaceholderConfigurer to load the properties from a custom location.

这里我们使用了PropertySourcesPlaceholderConfigurer来从一个自定义的位置加载属性。

7. Excluding a File From the Fat Jar

7.从脂肪罐中排除一个文件

The Maven Boot plugin will automatically include all files in the src/main/resources directory to the jar package.

Maven Boot插件会自动将src/main/resources目录下的所有文件纳入jar包。

If we don’t want a file to be part of the jar, we can use a simple configuration to exclude it:

如果我们不希望某个文件成为jar的一部分,我们可以使用一个简单的配置来排除它。

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>**/conf.properties</exclude>
            </excludes>
        </resource>
    </resources>
</build>

In this example, we’ve filtered out the conf.properties file from being included in the resulting jar.

在这个例子中,我们已经过滤掉了conf.properties文件,使其不包含在生成的jar中。

8. Conclusion

8.结语

This article showed how the Spring Boot framework itself takes care of externalized configuration for us.

本文展示了Spring Boot框架本身如何为我们处理外部化配置

Often, we just have to place the property values in the correct files and locations. But we can also use Spring’s Java API for more control.

通常情况下,我们只需要将属性值放在正确的文件和位置。但我们也可以使用Spring的Java API进行更多的控制。

As always, the full source code of the examples is available over on GitHub.

一如既往,这些示例的完整源代码可在GitHub上获得over