简体   繁体   English

Spring boot application.properties maven 多模块项目

[英]Spring boot application.properties maven multi-module projects

We are using spring boot in a multi-module project.我们在一个多模块项目中使用 spring boot。

We have a Domain access module which has the common domain object classes, repositories, together with configuration for the datasource, JPA, Hibernate, etc. These are configured using a application.properties.我们有一个域访问模块,它具有公共域对象类、存储库以及数据源、JPA、Hibernate 等的配置。这些是使用 application.properties 配置的。 We put all this configuration into the common module to save duplicating these common configurations in the higher level modules.我们将所有这些配置放入通用模块中,以节省在更高级别模块中重复这些通用配置。

This all works fine when building the domain module, so the configurations are loaded correctly in the test units.这在构建域模块时一切正常,因此在测试单元中正确加载了配置。

However the problems start when we try to use the domain module in the higher layer modules;然而,当我们尝试在更高层模块中使用域模块时,问题就开始了; they have their own application.properties which means Spring loads them and not the the Domain module application.properties, which this means the data source is not configured because only the higher module application.properties are loaded.它们有自己的 application.properties 这意味着 Spring 加载它们而不是域模块 application.properties,这意味着数据源没有配置,因为只有更高的模块 application.properties 被加载。

What we would like is both the domain module and higher level application properties to be loaded by Spring.我们想要的是由 Spring 加载的域模块和更高级别的应用程序属性。 But we can't see any easy way to do this.但是我们看不到任何简单的方法来做到这一点。

I'm thinking this must be a common problem, and wonder if there any recommended solutions for this problem?我想这一定是一个常见问题,不知道是否有针对此问题的推荐解决方案?

As we are using spring-boot the solution should ideally use annotations instead of applictionContext.xml.当我们使用 spring-boot 时,解决方案最好使用注释而不是 applictionContext.xml。

Maybe you should only use application.properties in the top-level aggregator project?也许您应该只在顶级聚合器项目中使用application.properties

You can always use @PropertySource in the child projects to configure them with a name that is specific to their use case.您始终可以在子项目中使用@PropertySource为它们配置特定于其用例的名称。

Or you can use different names for each project and glue them together in the top-level project using spring.config.location (comma-separated).或者您可以为每个项目使用不同的名称,并使用spring.config.location (逗号分隔)将它们粘合在顶级项目中。

I agree with @Dave Syer.我同意@Dave Syer。 The idea of splitting an application into multiple modules is that each of those is an independent unit, in this case a jar file.将应用程序拆分为多个模块的想法是,每个模块都是一个独立的单元,在这种情况下是一个 jar 文件。 Theoretically you could split each of those jar files into their own source repositories, and then use them across multiple projects.理论上,您可以将这些 jar 文件中的每一个拆分到它们自己的源存储库中,然后在多个项目中使用它们。 Let's say you want to reuse these domain classes in both a web and batch application, if all the APPLICATION level configuration is stored within each of the individual modules, it severely reduces their reusability.假设您想在 Web 和批处理应用程序中重用这些域类,如果所有 APPLICATION 级别的配置都存储在每个单独的模块中,则会严重降低它们的可重用性。

IMO only the aggregating module should contain all of the configuration necessary to run as an application, everything else is simply a dependency that can be remixed and reused as necessary. IMO 只有聚合模块应该包含作为应用程序运行所需的所有配置,其他一切都只是一个可以根据需要重新混合和重用的依赖项。

Maybe another approach could be to define specific profiles for each module and use the application.properties file just to specify which profiles are active using the spring.profiles.include property.也许另一种方法是为每个模块定义特定的配置文件,并使用 application.properties 文件仅使用spring.profiles.include属性指定哪些配置文件处于活动状态。

domain-module
- application.properties
- application-domain.properties

app-module
- application.properties
- application-app.properties

and into the application.properties file of app-module并进入 app-module 的 application.properties 文件

spring.profiles.include=domain,app

Another thing you can do (besides only using application.properties at the top-level as Dave Syer mentions) is to name the properties file of the domain module something like domainConfig.properties .您可以做的另一件事(除了像 Dave Syer 提到的那样仅在顶层使用 application.properties )是将域模块的属性文件命名为domainConfig.properties类的domainConfig.properties

That way you avoid the name clash with application.properties .这样你就可以避免与application.properties的名称冲突。

domainConfig.properties would contain all the data needed for the domain module to be able to tested on it's own. domainConfig.properties将包含域模块能够自行测试所需的所有数据。 The integration with the rest of the code can easily be done either using multiple @PropertySource (one for domainConfig.properties and one for application.properties ) or configuring a PropertySourcesPlaceholderConfigurer bean in your Java Config (check out this tutorial) that refers to all the needed property files可以使用多个@PropertySource (一个用于domainConfig.properties一个用于application.properties )或在 Java 配置中配置一个PropertySourcesPlaceholderConfigurer bean(查看教程)来轻松完成与其余代码的集成,该 bean 指的是所有需要的属性文件

in spring-boot since 2.4 support spring.config.import在 spring-boot 自2.4支持spring.config.import

eg例如

application.name=myapp
spring.config.import=developer.properties

# import from other module
spring.config.import=classpath:application-common.properties

or with spring.config.activate.on-profile或使用spring.config.activate.on-profile

spring.config.activate.on-profile=prod
spring.config.import=prod.properties

ref: https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4参考: https : //spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM