SpringBoot 多模块、多配置文件 application.yml 冲突

本贴最后更新于 1549 天前,其中的信息可能已经事过境迁

问题描述

  1. 模块 xinyue-admin 依赖 xinyue-service 模块
  2. xinyue-service 模块使用 jpa 连接数据库
  3. 若 dependency 中配置 jpa 的依赖,且 jpa 读取不到相关的数据库链接配置项则会报错
  4. 单独在 xinyue-service 中配置数据库连接配置项后测试,正常运行
  5. 在 xinyue-admin 引用 xinyue-service 的 xxRepository 测试,出现报错

报错信息:

2020-02-01 20:53:31.671  INFO 33488 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-02-01 20:53:31.671  INFO 33488 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1730 ms
2020-02-01 20:53:31.717  WARN 33488 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2020-02-01 20:53:31.720  INFO 33488 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-02-01 20:53:31.738  INFO 33488 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-02-01 20:53:31.749 ERROR 33488 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


Process finished with exit code 1

分析

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.(无法配置数据源:未指定'url'属性,并且无法配置任何嵌入式数据源。)

  • 根据提示猜想应该是 jpa 没有读到配置项的问题
  • 但是在 xinyue-service 中明明配置过了,且测试运行好好的
  • 所以猜想可能是由于模块间依赖,导致的 SpringBoot 不知道该读取哪个 application.yml 的问题

解决方案

尝试

根据分析,我们更改 xinyue-service 的 application.ymlapplication-service.yml,然后重新运行测试,结果还是不行!!!报错依旧。。。

改进思路

网上搜索了一些文章发现,默认 SpringBoot 只会加载 application.yml 的配置项

记得之前多环境配置文件 application-dev.yml/application-prod.yml 可以通过 spring.profiles.active=dev/prod 的方式切换不同环境的配置文件;那么是否可以通过这种方式来达到目的呢?

操作

  • 将 xinyue-service 的配置文件重命名为:application-service.yml
  • 在 xinyue-admin 的 application.yml 中启用指定配置文件:spring.profiles.active=service
  • 重启后,运行成功
  • 如果各个模块还需要做 dev/prod 环境的切换怎么办呢?
    • 亲测后,发现 spring.profiles.active 支持使用多配置文件
  • 故可以在 xinyue-admin 中有三个配置文件
    • application.yml
    • application-dev.yml
    • application-prod.yml
  • 也可以在 xinyue-service 中创建三个配置文件
    • application-service.yml
    • application-service-dev.yml
    • application-service-prod.yml
  • 然后再 application.yml 中根据情况配置,类似如下即可
    • spring.profiles.active=dev,service-dev

同时启用多配置文件,是否影响 @Value 取值

保险起见,防止上面的方式出现配置项在代码中 @Value 读取不到的情况,专门做了测试。

在 application-dev.yml 和 application-service-dev.yml 中分别配置一个属性,在代码中读取,亲测没有问题 😀

测试如图:

参考

  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    941 引用 • 1458 回帖 • 154 关注
  • 多模块
    2 引用 • 6 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
  • xinyue
    作者

    测试

  • 其他回帖
  • xinyue
    作者

    记录