简体   繁体   English

如何将Spring Boot中的application.properties文件外部化为外部依赖JAR?

[英]How to externalize application.properties files in Spring Boot to e.g. external dependent JAR?

I have a simple Maven module (not a Spring Boot application) in which I have placed my application.properties file. 我有一个简单的Maven模块(不是Spring Boot应用程序),我在其中放置了我的application.properties文件。

I have 6-7 Spring Boot applications and I don't want to have an application.properties file in each and every application directory. 我有6-7春天启动的应用程序,我不希望有一个application.properties中的每个文件和每一个应用程序目录。 I prefer it, if it is at one single place (external Maven module). 我更喜欢它,如果它在一个地方(外部Maven模块)。

I am adding the maven module as a dependency in each of those Spring Boot application poms. 我将maven模块添加为每个Spring Boot应用程序poms中的依赖项。

But, when I run those applications, it is not able to auto-detect the application.properties file because it is coming from a dependent jar not present physically in each of their application directories. 但是,当我运行这些应用程序时,它无法自动检测application.properties文件,因为它来自物理上不存在于每个应用程序目录中的依赖jar。

Is there any way to make this possible? 有没有办法让这成为可能? I would like to avoid having properties files in 6-7 different locations, because that becomes tough to manage and handle. 我想避免在6-7个不同的位置拥有属性文件,因为这很难管理和处理。

Thank you in advance! 先感谢您!

Consider using Spring Cloud Config that provides server and client-side support for externalized configuration in a distributed system. 考虑使用Spring Cloud Config ,它为分布式系统中的外部化配置提供服务器和客户端支持。 It requires some small effort, but it is very useful in long term. 它需要一些小的努力,但从长远来看它非常有用。 Config server manages configuration files ( .properties or .yml ), you can still use different config per profile (eg application-test.properties , application-prod.properties etc.). 配置服务器管理配置文件( .properties.yml ),您仍然可以为每个配置文件使用不同的配置(例如application-test.propertiesapplication-prod.properties等)。 Your application has a higher priority, so you can always override properties coming from config server if needed. 您的应用程序具有更高的优先级,因此如果需要,您始终可以覆盖来自配置服务器的属性。 Another cool feature is that config server can utilize Git repository, so you can easily version all your configuration files. 另一个很酷的功能是配置服务器可以使用Git存储库,因此您可以轻松地对所有配置文件进行版本控制。 It also supports encryption - any fragile data can be encrypted so only your application knows how to decrypt it. 它还支持加密 - 任何脆弱的数据都可以加密,因此只有您的应用程序知道如何解密它。

Config server 配置服务器

Config server is nothing else than a simple Spring Boot application that can be implemented as: 配置服务器只不过是一个简单的Spring Boot应用程序,它可以实现为:

@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServer.class, args);
  }
}

with simple application.properties file included: 包含简单的application.properties文件:

server.port: 8888
spring.cloud.config.server.git.uri: file://${user.home}/config-repo

with dependency in pom.xml pom.xml中的依赖关系

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

Config client 配置客户端

On client side you add a dependency to your pom.xml (or its equivalent in build.gradle if you use Gradle): 在客户端,您可以为pom.xml添加依赖项(如果使用Gradle,则在build.gradle中添加相应的依赖项):

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>

and all you have to do is add a URL to config server to your application.properties (or application.yml if you use YAML insted): 和所有你需要做的就是添加一个URL来配置服务器,以您的application.properties(或者,如果你使用YAML insted的application.yml):

spring.cloud.config.uri: http://myconfigserver.com

Config files structure 配置文件结构

Now let's say you have set up Git repository for your configuration files. 现在假设您已经为配置文件设置了Git存储库。 Let's assume that your applications are named like horus , venus , mercury etc. and you have 3 different profiles: dev , test and prod . 让我们假设您的应用程序被命名为horusvenusmercury等,并且您有3种不同的配置文件: devtestprod You also have some configuration that is common for all applications. 您还有一些适用于所有应用程序的配置。 In this case your configuration files structure would look like this (I will use properties files here but it applies to YAML as well): 在这种情况下,您的配置文件结构将如下所示(我将在此处使用属性文件,但它也适用于YAML):

  • application.properties - common config for all apps no matter what profile they use application.properties - 所有应用application.properties通用配置,无论他们使用什么配置文件
  • application-dev.properties - common config for all apps running with dev profile application-dev.properties - 使用dev配置文件运行的所有应用application-dev.properties通用配置
  • application-test.properties - common config for all apps running with test profile application-test.properties - 使用test配置文件运行的所有应用application-test.properties通用配置
  • application-prod.properties - common config for all apps running with prod profile application-prod.properties - 使用prod配置文件运行的所有应用application-prod.properties通用配置
  • horus.properties - horus app config for, common for all profiles horus.properties - horus app config for,适用于所有配置文件
  • horus-dev.properties - horus app config for dev profile only horus-dev.properties - 仅用于dev配置文件的horus app配置
  • horus-test.properties - horus app config for test profile only horus-test.properties - 仅用于test配置文件的horus app配置
  • horus-prod.properties - horus app config for prod profile only horus-prod.properties - 仅用于prod配置文件的horus app配置
  • etc. 等等

There are some additional options that can be set (like encryption, connection strategy (fail fast or ignore) etc.), everything is well described and documented in official documentation https://cloud.spring.io/spring-cloud-config/ Hope it helps you making a good choice for managing your configuration in distributed application environment. 还有一些其他选项可以设置(如加密,连接策略(快速失败或忽略)等),一切都在官方文档https://cloud.spring.io/spring-cloud-config/中有详细描述和记录。希望它可以帮助您在分布式应用程序环境中管理配置。 Config server is a solution that was invented to solve this problem. 配置服务器是为解决此问题而发明的解决方案。

While Szymon Stepniak's answer certainly is a "by-the-book" of Spring Boot answer, I understand your situation, and even tried to do what you try to do by myself. 虽然Szymon Stepniak的答案肯定是Spring Boot答案的“书本”,但我了解你的情况,甚至尝试自己做你想做的事情。 Indeed, you can't define application.properties in other "external modules". 实际上,您无法在其他“外部模块”中定义application.properties。

Here is how I've solved it: 以下是我如何解决它:

  1. Create a configuration in the "common" module 在“common”模块中创建配置
  2. Create a property file in src/main/resources . src/main/resources创建属性文件。 It shouldn't be named application properties, It's better to provide a unique name for it (at least this is how I've done it, so let's assume that the file is called application-common.properties ) 它不应该被命名为应用程序属性,最好为它提供一个唯一的名称(至少我是这样做的,所以我们假设该文件名为application-common.properties

  3. Use @PropertySources annotation to define a property file and load it with configuration. 使用@PropertySources批注定义属性文件并使用配置加载它。

Here is an example: 这是一个例子:

package com.myapp.common;
@Configuration
@PropertySources({
    @PropertySource("classpath:application-common.properties")
})
public class MyConfiguration {
    // you don't really have to define beans 
}

Now if you want this configuration to load automatically only because the dependency is defined in your spring boot module of your build system, I've found the best to utilize spring factories: 现在,如果您希望仅在构建系统的spring boot模块中定义依赖项时自动加载此配置,我发现最好使用spring工厂:

  1. Create the file src/main/resources/META-INF/spring.factories 创建文件src/main/resources/META-INF/spring.factories
  2. Place the following into this file: 将以下内容放入此文件中:

     org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\ com.myapp.common.MyConfiguration 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Spring Boot application.properties外部化到tomcat / lib文件夹 - How to externalize Spring Boot application.properties to tomcat/lib folder 如何在Spring Boot中的application.properties中指定外部属性文件? - How to specify external properties files in application.properties in Spring Boot? Spring Boot - 从依赖的 jar 加载 application.properties/yml - Spring Boot - Load application.properties/yml from dependent jar 在jar spring boot外部目录中搜索application.properties - Search application.properties in directory external to jar spring boot 在Tomcat中为Spring Boot应用程序访问externalize application.properties? - access externalize application.properties in Tomcat for Spring boot application? spring boot jar中的application.properties文件在哪里? - Where are the application.properties files in a spring boot jar? Spring 引导无法使用依赖的应用程序。属性 - Spring Boot not working with dependent application.properties Spring 引导类路径配置文件覆盖外部 application.properties - Spring boot classpath configuration files overrides external application.properties 作为WAR在Springcat上部署的Spring Boot,如何针对不同配置文件(Dev,Test,Staging,Prod)外部化application.properties - Spring boot deployed as WAR on tomcat, How to Externalize application.properties for different profiles(Dev,Test,Staging,Prod) Spring Boot 外部应用程序.properties - Spring boot external application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM