Multi-configuration File Problem in springboot Multi-module Case

  java, question

A springboot project, managed by mavan, hopes to be divided into modules.
For example, it is divided into service, admin-web, front-web and interface, and each module is an independent maven project.
Service is the lowest public service, providing business logic processing, database storage, etc.
The other three are control layers, providing page display, interfaces, etc.

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

The general project structure is as follows:
图片描述
图片描述

For example, the database configuration file is placed in the yml of the service, the relevant configuration of the page thymeleaf is placed in the yml of the web, and the web depends on the service. At present, spring.profiles.include can be used in the yml of the web to introduce the corresponding yml file into the project, and the configuration files of each module can be independent of each other, but there are some problems:

  1. The service profile format must be application-xxx.yml
  2. The configuration include value that must be displayed in the yml of the web

Question: Can an @Configuration be provided in the service to inject the attribute value of yml in the service into the spring container, instead of displaying the yml file introduced into the service in the yml of the web, in which case it is only necessary to rely on the service in the pom of the web, and the configuration file does not need to be processed


Project code:
https://gitee.com/soft_xiang/ …

The database is a public network test library, delete data at any time, and do not behave in such a way.

= = = 2018.06.19 update = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The problem of loading sub-module configuration files has been preliminarily 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 yet. All of Passer Passer’s people have a look at it.
= = = 2018.06.19 update end = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Using application.yml configuration is nothing more than producing bean, as in your sample code.application-module1-service.yml:

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

This can be implemented in coding as follows:

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