Spring Boot 配置 jar 包外面的 Properties 配置文件(springboot配置文件放到jar外面,怎么读取)

  本篇文章为你整理了Spring Boot 配置 jar 包外面的 Properties 配置文件(springboot配置文件放到jar外面,怎么读取)的详细内容,包含有springboot外置jar和配置文件 springboot配置文件放到jar外面,怎么读取 springboot打成jar包后修改配置文件 spring boot支持将配置文件外置到包外 Spring Boot 配置 jar 包外面的 Properties 配置文件,希望能帮助你了解 Spring Boot 配置 jar 包外面的 Properties 配置文件。

  Properties 文件是我们可以用来存储项目特定信息的常用方法。理想情况下,我们应该将其保留在 jar 包之外,以便能够根据需要对配置进行更改。

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

  二、使用默认位置

  按照惯例,Spring Boot 按以下优先顺序在四个预定位置查找外部化配置文件 --- application.properties 或 application.yml :{#crayon-5c73a186c8530009937282}

  当前目录的 /config 子目录

  一个类路径 /config 包

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

  三、使用命令行

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

  

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

 

  

 

  我们还可以传递应用程序搜索文件的文件夹位置:

  

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

 

  

 

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

  在那里,我们可以使用 -D 参数:

  

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

 

  

 

  四、使用环境变量

  现在假设我们不能更改启动命令。

  很棒的是 Spring Boot 还会读取环境变量 SPRING_CONFIG_NAME 和 SPRING_CONFIG_LOCATION:

  

export SPRING_CONFIG_NAME=application,jdbc

 

  export SPRING_CONFIG_LOCATION=file:///Users/home/config

  java -jar app.jar

  

 

  请注意,仍将加载默认文件。但是环境特定的属性文件优先以防发生属性冲突。

  使用应用程序属性

  
如我们所见,我们必须在应用程序启动之前定义 spring.config.name 和 spring.config.location 属性,因此在 application.properties 文件(或 YAML 对应文件)中使用它们将没有影响。

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

  与此更改一起,团队引入了一个新属性,允许直接从应用程序属性导入其他配置文件:

  

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

 

  

 

  以编程方式

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

  

public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {

 

   PropertySourcesPlaceholderConfigurer properties =

   new PropertySourcesPlaceholderConfigurer();

   properties.setLocation(new FileSystemResource("/Users/home/conf.properties"));

   properties.setIgnoreResourceNotFound(false);

   return properties;

  

 

  在这里,我们使用 PropertySourcesPlaceholderConfigurer 从自定义位置加载属性。

  七、从 Fat Jar 中排除文件

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

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

  

 build 

 

   resources

   resource

   directory src/main/resources /directory

   filtering true /filtering

   excludes

   exclude **/conf.properties /exclude

   /excludes

   /resource

   /resources

   /build

  

 

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

  本文展示了 Spring Boot 框架本身如何为我们处理 externalized configuration。

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

  与往常一样,示例的完整源代码可在 GitHub 上 获得。

  以上就是Spring Boot 配置 jar 包外面的 Properties 配置文件(springboot配置文件放到jar外面,怎么读取)的详细内容,想要了解更多 Spring Boot 配置 jar 包外面的 Properties 配置文件的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: