SpringBoot 多模块 配置覆盖问题

因为现在手上有个项目,在未拆分成微服务之前,会有多个层级的模块依赖,如下图:

依赖关系图

现在有个问题,我在common里面配置了一个属性值叫做:project.mark=123, 在 biz module1 中需要它的值是 456,在ui-module1中需要这个值是789.

这种情况,可以配置profile 或者 properties+yml文件来实现。

properties+yml:

如果一个classpath中,同时存在application.yml和application.properties,那么properties的优先级是大于yml的。也就是说properties中的内容会覆盖yml中的内容。请参看其他博友的文章:https://blog.csdn.net/it_faquir/article/details/80869578

profile:

需要在application.properties/yml中级目录下创建一个application-<profile>.properties/yml文件。并在application.properties/yml中启用这个profile就行了。请百度搜索下spring boot profile。

如果使用了
EnableConfigurationProperties(Your configuration bean.class),那么可以在application-<profile>.properties/yml中直接以

Your configuration bean.class上面定义的ConfigurationProperties("xxx")中的xxx这个值进行覆盖。

例子:

common模块中的配置文件

common模块中的配置信息,这个时候,workerId的值是1

UI模块中对common配置的引用

这是在UI模块中去覆盖common模块中的配置值。workerId的值变成2。注意,这里覆盖的时候,是用tsc.模块名.该模块下的对应属性,如果有驼峰命名,则全小写,并加-分割。

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot模块项目的自动配置,可以通过在父模块的 pom.xml 文件中添加 Spring Boot 的 Maven 插件来完成。具体步骤如下: 1. 在父模块的 pom.xml 文件中添加 Spring Boot 的 Maven 插件: ```xml <build> <plugins> <!-- Spring Boot Maven Plugin --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring.boot.version}</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 2. 在每个子模块的 pom.xml 文件中添加对父模块依赖: ```xml <parent> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> ``` 3. 在子模块的 src/main/resources 目录下添加 application.properties 或 application.yml 配置文件,通过配置文件来进行自动配置。 例如,如果我们要在子模块中使用 Spring Data JPA,可以在 application.properties 或 application.yml 中添加如下配置: ``` spring.jpa.database=MYSQL spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root ``` 通过这些配置,我们可以自动配置 Spring Data JPA 并连接到指定的 MySQL 数据库。 需要注意的是,在多模块项目中,每个子模块可以有自己的配置文件,但是父模块中的配置文件不能被子模块覆盖。如果需要在子模块覆盖模块中的配置,可以通过在子模块中使用 @PropertySource 注解来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

easewalk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值