diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/SopAdminEnvironmentPostProcessor.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/SopAdminEnvironmentPostProcessor.java new file mode 100644 index 00000000..cd87aaf0 --- /dev/null +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/SopAdminEnvironmentPostProcessor.java @@ -0,0 +1,40 @@ +package com.gitee.sop.adminserver.config; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.PropertiesPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; + +import java.io.IOException; +import java.util.Properties; + +/** + * 自定义环境处理,在运行SpringApplication之前加载任意配置文件到Environment环境中 + * https://www.jianshu.com/p/be6c818fe6ff + */ +public class SopAdminEnvironmentPostProcessor implements EnvironmentPostProcessor { + + private final Properties properties = new Properties(); + + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + Resource resource = new ClassPathResource("sop-admin.properties"); + // 加载成PropertySource对象,并添加到Environment环境中 + environment.getPropertySources().addLast(loadProfiles(resource)); + } + + private PropertySource loadProfiles(Resource resource) { + if (resource == null || !resource.exists()) { + throw new IllegalArgumentException("资源" + resource + "不存在"); + } + try { + properties.load(resource.getInputStream()); + return new PropertiesPropertySource(resource.getFilename(), properties); + } catch (IOException ex) { + throw new IllegalStateException("加载配置文件失败" + resource, ex); + } + } +} diff --git a/sop-admin/sop-admin-server/src/main/resources/META-INF/spring.factories b/sop-admin/sop-admin-server/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..201aa743 --- /dev/null +++ b/sop-admin/sop-admin-server/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +# 自定义自动配置类,如果有多个类,使用逗号(,)分隔,\正斜杠标示换行还可以读取到指定的类 +org.springframework.boot.env.EnvironmentPostProcessor=com.gitee.sop.adminserver.config.SopAdminEnvironmentPostProcessor diff --git a/sop-admin/sop-admin-server/src/main/resources/application-dev.properties b/sop-admin/sop-admin-server/src/main/resources/application-dev.properties index 2588e1a7..917d0f92 100644 --- a/sop-admin/sop-admin-server/src/main/resources/application-dev.properties +++ b/sop-admin/sop-admin-server/src/main/resources/application-dev.properties @@ -1,7 +1,5 @@ server.port=8082 -spring.application.name=sop-admin -# ------- 需要改的配置 ------- # mysql数据库账号 mysql.host=localhost:3306 mysql.username=root @@ -9,33 +7,5 @@ mysql.password=root # nacos注册中心地址 nacos.url=127.0.0.1:8848 -# ------- 需要改的配置end ------- - -# session过期时间,分钟 -admin.access-token.timeout-minutes=30 -# 签名方式,rsa:支付宝开放平台签名方式,md5:淘宝开放平台签名方式 -sop.sign-type=rsa - -# nacos配置 -nacos.config.server-addr=${nacos.url} -nacos.discovery.server-addr=${nacos.url} - -# 数据库配置 -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.url=jdbc:mysql://${mysql.host}/sop?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai -spring.datasource.username=${mysql.username} -spring.datasource.password=${mysql.password} - -# 固定不用改 -mybatis.mapper-locations=classpath:mybatis/mapper/*.xml -easyopen.show-doc=false -easyopen.ignore-validate=true logging.level.com.gitee=debug - -# 不用改 -mybatis.fill.com.gitee.fastmybatis.core.support.DateFillInsert=gmt_create -mybatis.fill.com.gitee.fastmybatis.core.support.DateFillUpdate=gmt_modified - -# 不用改,如果要改,请全局替换修改 -sop.secret=MZZOUSTua6LzApIWXCwEgbBmxSzpzC diff --git a/sop-admin/sop-admin-server/src/main/resources/sop-admin.properties b/sop-admin/sop-admin-server/src/main/resources/sop-admin.properties new file mode 100644 index 00000000..a98965a0 --- /dev/null +++ b/sop-admin/sop-admin-server/src/main/resources/sop-admin.properties @@ -0,0 +1,32 @@ +# 这里的配置不用改,如果要改在application-xx.properties中改 +# 在application-xx.properties中配置会覆盖这里的值 +# 参考:com.gitee.sop.adminserver.config.SopAdminEnvironmentPostProcessor + +spring.application.name=sop-admin + +# session过期时间,分钟 +admin.access-token.timeout-minutes=30 +# 签名方式,rsa:支付宝开放平台签名方式,md5:淘宝开放平台签名方式 +sop.sign-type=rsa + +# nacos配置 +nacos.config.server-addr=${nacos.url} +nacos.discovery.server-addr=${nacos.url} + +# 数据库配置 +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.datasource.url=jdbc:mysql://${mysql.host}/sop?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai +spring.datasource.username=${mysql.username} +spring.datasource.password=${mysql.password} + +# 固定不用改 +mybatis.mapper-locations=classpath:mybatis/mapper/*.xml +easyopen.show-doc=false +easyopen.ignore-validate=true + +# 不用改 +mybatis.fill.com.gitee.fastmybatis.core.support.DateFillInsert=gmt_create +mybatis.fill.com.gitee.fastmybatis.core.support.DateFillUpdate=gmt_modified + +# 不用改,如果要改,请全局替换修改 +sop.secret=MZZOUSTua6LzApIWXCwEgbBmxSzpzC diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/env/SopEnvironmentPostProcessor.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/config/SopGatewayEnvironmentPostProcessor.java similarity index 91% rename from sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/env/SopEnvironmentPostProcessor.java rename to sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/config/SopGatewayEnvironmentPostProcessor.java index a4122b3a..9ea1cbc1 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/env/SopEnvironmentPostProcessor.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/config/SopGatewayEnvironmentPostProcessor.java @@ -1,4 +1,4 @@ -package com.gitee.sop.gatewaycommon.env; +package com.gitee.sop.gatewaycommon.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; @@ -14,7 +14,7 @@ import java.util.Properties; /** * 自定义环境处理,在运行SpringApplication之前加载任意配置文件到Environment环境中 */ -public class SopEnvironmentPostProcessor implements EnvironmentPostProcessor { +public class SopGatewayEnvironmentPostProcessor implements EnvironmentPostProcessor { private final Properties properties = new Properties(); diff --git a/sop-gateway/src/main/resources/META-INF/spring.factories b/sop-gateway/src/main/resources/META-INF/spring.factories index c0f0b43f..c7499f27 100644 --- a/sop-gateway/src/main/resources/META-INF/spring.factories +++ b/sop-gateway/src/main/resources/META-INF/spring.factories @@ -1,3 +1,3 @@ -org.springframework.boot.env.EnvironmentPostProcessor=com.gitee.sop.gatewaycommon.env.SopEnvironmentPostProcessor # 自定义自动配置类,如果有多个类,使用逗号(,)分隔,\正斜杠标示换行还可以读取到指定的类 +org.springframework.boot.env.EnvironmentPostProcessor=com.gitee.sop.gatewaycommon.config.SopGatewayEnvironmentPostProcessor org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gitee.sop.bridge.SopGatewayAutoConfiguration