Spring boot application.properties maven 多模块项目 [英] Spring boot application.properties maven multi-module projects

查看:100
本文介绍了Spring boot application.properties maven 多模块项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在一个多模块项目中使用 spring boot.

We are using spring boot in a multi-module project.

我们有一个域访问模块,它具有公共域对象类、存储库以及数据源、JPA、Hibernate 等的配置.这些是使用 application.properties 配置的.我们将所有这些配置都放到了通用模块中,以避免在更高级别的模块中重复这些通用配置.

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. 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.

然而,当我们尝试在高层模块中使用域模块时,问题就开始了;它们有自己的 application.properties 这意味着 Spring 加载它们而不是域模块 application.properties,这意味着数据源没有配置,因为只有更高的模块 application.properties 被加载.

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.

我们想要的是由 Spring 加载的域模块和更高级别的应用程序属性.但是我们看不到任何简单的方法来做到这一点.

What we would like is both the domain module and higher level application properties to be loaded by 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?

当我们使用 spring-boot 时,解决方案最好使用注解而不是 applictionContext.xml.

As we are using spring-boot the solution should ideally use annotations instead of applictionContext.xml.

推荐答案

也许你应该只在顶级聚合器项目中使用 application.properties ?

Maybe you should only use application.properties in the top-level aggregator project?

您始终可以在子项目中使用 @PropertySource 为它们配置特定于其用例的名称.

You can always use @PropertySource in the child projects to configure them with a name that is specific to their use case.

或者您可以为每个项目使用不同的名称,并使用 spring.config.location(逗号分隔)将它们粘合在顶级项目中.

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 boot application.properties maven 多模块项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆