The problem of multiple profiles in the case of multiple modules in springboot

A springboot project, managed by mavan, wants to be modularized.
for example, each module is divided into service, admin-web, front-web, and interface,. Each module is a separate maven project.
service is the lowest level public service, providing business logic processing, database storage, etc.
the other three are control layer, providing page display, interface, etc.

now the question: how to merge two files when each module has its own configuration file application.yml, integration, for example, when starting admin-web, load application.yml in service

the approximate project structure is as follows:

for example, the database configuration file is placed in the yml of service, and the thymeleaf-related configuration of the page is placed in the yml of web. Web relies on service, to use spring.profiles.include in the yml of web to introduce the corresponding yml file in service into the project, and each module configuration file can be independent of each other, but there are some problems:

  1. service configuration file format must be application-xxx.yml
  2. configuration values that must be displayed in the yml of web

question: can you provide a @ Configuration, in service that can inject the attribute values of yml in service into the spring container without the need to display the yml file that introduces service in the yml of web? in this case, you only need to rely on service in the pom of web, and the configuration file does not need to be processed.

< hr >

Project Code:
https://gitee.com/soft_xiang/.

Database is a public network test library. Delete data at any time, don"t mess around

= 2018.06.19 Update =
the problem of loading the sub-module configuration file has been initially solved. See my answer and recommend the second method

the loading problem of multi-environment configuration files caused by the second method has not been solved for the time being. Please take a look at
= 2018.06.19 update end=

for those who pass by.
Mar.20,2021

the EnvironmentPostProcessorExample, on the official website of spring boot can solve all your problems perfectly.
Spring Boot Reference Guide 2.1.1.RELEASE
76.3 Customize the Environment or ApplicationContext Before
It Starts


configuring with application.yml is nothing more than producing bean, such as application-module1-service.yml :

in your sample code.
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://140.143.26.196:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true
    username: test
  

can be implemented in coding like this:

@Bean
@Primary
public DataSource dataSource() {
    return DataSourceBuilder
        .create()
        .username("")
        .password("")
        .url("")
        .driverClassName("")
        .build();
}

< H1 > the first method < / H1 >

try to add custom datasource to module1-service (only properties,Yml may be able to implement, but not quite know how to write). It can be implemented (it is not clear whether it will affect the original spring datasource mechanism, but is not familiar with the principle of spring)
code is as follows:

  https://gitee.com/soft_xiang/.

however, there will be circular dependencies
running code will have

SpringContextUtil. Not loaded.

it is not implemented to load the corresponding configuration file according to active

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3b931-2c2b1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3b931-2c2b1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?