子模块1配置信息如下
子模块2配置信息如下
问题描述
需求
项目案例图
代码说明
PrintConfig
package com.simple.config;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class PrintConfig {
private static final List<String> UN_PRINT = Arrays.asList("java.class.path", "config.md5Key", "config.rc4Key", "druid.password", "redis.password", "jasypt.encryptor.password");
public static void printApplicationProperties(ConfigurableEnvironment environment) {
StreamSupport.stream(
environment.getPropertySources().spliterator(), false)
.filter(propertySource -> (propertySource instanceof EnumerablePropertySource))
.map(propertySource -> ((EnumerablePropertySource) propertySource).getPropertyNames())
.flatMap(Arrays::stream).distinct().collect(Collectors.toMap(Function.identity(), environment::getProperty))
.entrySet().stream().forEach(entry -> {
String key = entry.getKey();
if (!UN_PRINT.contains(key)) {
if(key.startsWith("custom.")) {
System.err.println(String.format("properties,[%s = %s] . ", key, entry.getValue()));
}
}
}
);
}
}
SystemStartInit
package com.simple.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;
/**
* 系统启动后初始化,此方法会在服务器启动后(已经开放端口),加载配置信息
*/
@Component
public class SystemStartInit implements CommandLineRunner {
@Autowired
private ConfigurableEnvironment environment;
@Override
public void run(String... args) throws Exception {
PrintConfig.printApplicationProperties(environment);
}
}
Module1Application
package com.simple;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Module1Application {
public static void main(String[] args) {
SpringApplication.run(Module1Application.class, args);
}
}
application-common.properties
# 公有属性,无需区分环境
custom.common.name=common-name
custom.common.name2=common-name2
application-mysql-dev.properties
# mysql-dev环境
custom.mysql.name=mysql-dev
custom.mysql.name2=mysql-dev2
application-mysql-prod.properties
# mysql-prod环境
custom.mysql.name=mysql-prod
custom.mysql.name2=mysql-prod2
application-redis-dev.properties
# redis-dev环境
custom.redis.name=redis-dev
custom.redis.name2=redis-dev2
application-redis-prod.properties
# redis-prod环境
custom.redis.name=redis-prod
custom.redis.name2=redis-prod2
application.properties
# 项目名称
spring.application.name=module1
# 端口号
server.port=8080
# 运行环境
spring.profiles.active=@environment@
# 引入所需要的模块信息 (可选择性的引入 :可只引入redis、mysql、common等)
# 引入后若想覆盖配置,则可在application-@environment@.properties 进行重写,不可在本配置文件进行重写
spring.profiles.include=common,mysql-@environment@,redis-@environment@
# 可以新增模块的私有参数
custom.module1.test=custom.module1.test
application-dev.properties
# 此配置可选,文件可放到 resources/config文件夹下,因放到config下会失去提示,故案例放在resources下
# 可以复写 base-config 里面的配置参数信息
custom.mysql.name=override-mysql-custom-dev
custom.common.name=override-common-name-custom-dev
# 可以配置模块的私有参数
custom.module1.name=private-module1-name-dev
application-prod.properties
# 此配置可选,文件可放到 resources/config文件夹下,因放到config下会失去提示,故案例放在resources下
# 可以复写 base-config 里面的配置参数信息
custom.mysql.name=override-mysql-custom-prod
custom.common.name=override-common-name-custom-prod
# 可以配置模块的私有参数
custom.module1.name=private-module1-name-prod
config.pom
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.simplegroupId>
<artifactId>configartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>module-1module>
<module>base-configmodule>
modules>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.0.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
base-config.pom
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>configartifactId>
<groupId>com.simplegroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>base-configartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
dependency>
dependencies>
project>
module-1.pom
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>configartifactId>
<groupId>com.simplegroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>module-1artifactId>
<dependencies>
<dependency>
<groupId>com.simplegroupId>
<artifactId>base-configartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
<profiles>
<profile>
<id>devid>
<properties>
<environment>devenvironment>
properties>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
profile>
<profile>
<id>prodid>
<properties>
<environment>prodenvironment>
properties>
profile>
profiles>
project>