You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
WhiteListManageSys/src/main/java/com/nmgs/MyEnvironmentPostProcessor....

85 lines
5.7 KiB
Java

package com.nmgs;
import com.nmgs.util.PathUtil;
import com.trkf.PasswordEncryption.PassWordUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
@Configuration
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication application) {
System.out.println("WhiteListManageSys读取公用配置文件======");
//公用配置文件路径
String path = PathUtil.applicationPath;
//本项目配置文件路径
String NewPath = PathUtil.projectApplicationPath;
System.out.println("Loading local settings from : " + path);
System.out.println("Loading local settings from : " + NewPath);
Properties properties = new Properties();
Properties Newproperties = new Properties();
try {
File fileproperties = new File(path);
File fileNewproperties = new File(NewPath);
// 判断文件夹是否存在
if (!fileproperties.exists()) {
fileproperties.createNewFile();
}
if (!fileNewproperties.exists()) {
fileNewproperties.createNewFile();
}
properties.load(new FileInputStream(path));
String DBType = properties.getProperty("WhiteDBType") == null ? "1" : properties.getProperty("WhiteDBType");
Newproperties.load(new FileInputStream(NewPath));
properties.setProperty("spring.logback.logPath",PathUtil.TomcatPath+"/logs");
properties.setProperty("spring.datasource.url",properties.getProperty("WhiteDBUrl"));
properties.setProperty("spring.datasource.username",properties.getProperty("WhiteDBUserName"));
properties.setProperty("spring.datasource.password",PassWordUtils.decrypt(properties.getProperty("WhiteDBPassWord")));
if(DBType.equals("1")){
properties.setProperty("spring.datasource.driver-class-name",Newproperties.getProperty("sqlClassName"));
properties.setProperty("spring.datasource.validationQuery",Newproperties.getProperty("sqlvalidationQuery"));
properties.setProperty("spring.datasource.hikari.connection-test-query",Newproperties.getProperty("sqlvalidationQuery"));
}else if(DBType.equals("2")){
properties.setProperty("spring.datasource.driver-class-name",Newproperties.getProperty("OracleClassName"));
properties.setProperty("spring.datasource.validationQuery",Newproperties.getProperty("OraclevalidationQuery"));
properties.setProperty("spring.datasource.hikari.connection-test-query",Newproperties.getProperty("OraclevalidationQuery"));
}else if(DBType.equals("3")){
properties.setProperty("spring.datasource.driver-class-name",Newproperties.getProperty("DMClassName"));
properties.setProperty("spring.datasource.validationQuery",Newproperties.getProperty("DMvalidationQuery"));
properties.setProperty("spring.datasource.hikari.connection-test-query",Newproperties.getProperty("DMvalidationQuery"));
}else if(DBType.equals("4")){
properties.setProperty("spring.datasource.driver-class-name",Newproperties.getProperty("MySQLClassName"));
properties.setProperty("spring.datasource.validationQuery",Newproperties.getProperty("MySQLvalidationQuery"));
properties.setProperty("spring.datasource.hikari.connection-test-query",Newproperties.getProperty("MySQLvalidationQuery"));
} else if (DBType.equals("5")) {
properties.setProperty("spring.datasource.driver-class-name", Newproperties.getProperty("MySQLGOADENDBClassName"));
properties.setProperty("spring.datasource.validationQuery", Newproperties.getProperty("MySQLGOADENDBvalidationQuery"));
properties.setProperty("spring.datasource.hikari.connection-test-query", Newproperties.getProperty("MySQLGOADENDBvalidationQuery"));
}
System.out.println("---------------------------specialEvent本次载入数据库----------------------");
System.out.println("server.port: " + properties.getProperty("server.port"));
System.out.println(" url : " + properties.getProperty("spring.datasource.url"));
System.out.println(" username : " + properties.getProperty("spring.datasource.username"));
System.out.println(" password : " + properties.getProperty("spring.datasource.password"));
System.out.println(" driver-class-name : " + properties.getProperty("spring.datasource.driver-class-name"));
//环境名称随意取,但尽量不能和其他环境名称相同,避免不生效
PropertiesPropertySource propertySource = new PropertiesPropertySource("environmentPostProcessor", properties);
PropertiesPropertySource newPropertySource = new PropertiesPropertySource("newEnvironmentPostProcessor", Newproperties);
//外部的文件是最先导入的
configurableEnvironment.getPropertySources().addFirst(propertySource);
//如果公用配置文件没有配置的则使用本项目的配置文件
configurableEnvironment.getPropertySources().addLast(newPropertySource);
} catch (Exception e) {
e.printStackTrace();
}
}
}