mirror of https://gitee.com/stylefeng/roses
【7.0.2】【rule】更新properties的快速获取
parent
910166a496
commit
be0625a043
|
@ -0,0 +1,36 @@
|
||||||
|
package cn.stylefeng.roses.kernel.rule.context;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* application.yml或application.properties配置的快速获取
|
||||||
|
* <p>
|
||||||
|
* 此类的使用必须激活 ConfigInitListener
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2021/2/26 18:27
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
public class ApplicationPropertiesContext {
|
||||||
|
|
||||||
|
private static final ApplicationPropertiesContext applicationPropertiesContext = new ApplicationPropertiesContext();
|
||||||
|
|
||||||
|
private String applicationName = null;
|
||||||
|
private String contextPath = null;
|
||||||
|
private String profile = null;
|
||||||
|
|
||||||
|
private ApplicationPropertiesContext() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initConfigs(ConfigurableEnvironment configurableEnvironment) {
|
||||||
|
applicationName = configurableEnvironment.getProperty("spring.application.name");
|
||||||
|
contextPath = configurableEnvironment.getProperty("server.servlet.context-path");
|
||||||
|
profile = configurableEnvironment.getProperty("spring.profiles.active");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ApplicationPropertiesContext getInstance() {
|
||||||
|
return applicationPropertiesContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -32,6 +32,7 @@ import cn.stylefeng.roses.kernel.config.api.context.ConfigContext;
|
||||||
import cn.stylefeng.roses.kernel.config.api.exception.ConfigException;
|
import cn.stylefeng.roses.kernel.config.api.exception.ConfigException;
|
||||||
import cn.stylefeng.roses.kernel.config.api.exception.enums.ConfigExceptionEnum;
|
import cn.stylefeng.roses.kernel.config.api.exception.enums.ConfigExceptionEnum;
|
||||||
import cn.stylefeng.roses.kernel.config.modular.factory.SysConfigDataFactory;
|
import cn.stylefeng.roses.kernel.config.modular.factory.SysConfigDataFactory;
|
||||||
|
import cn.stylefeng.roses.kernel.rule.context.ApplicationPropertiesContext;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
|
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
|
@ -76,8 +77,12 @@ public class ConfigInitListener implements ApplicationListener<ApplicationContex
|
||||||
// 初始化Config Api
|
// 初始化Config Api
|
||||||
ConfigContext.setConfigApi(new ConfigContainer());
|
ConfigContext.setConfigApi(new ConfigContainer());
|
||||||
|
|
||||||
|
// 获取environment参数
|
||||||
ConfigurableEnvironment environment = applicationContextInitializedEvent.getApplicationContext().getEnvironment();
|
ConfigurableEnvironment environment = applicationContextInitializedEvent.getApplicationContext().getEnvironment();
|
||||||
|
|
||||||
|
// 初始化ApplicationPropertiesContext
|
||||||
|
ApplicationPropertiesContext.getInstance().initConfigs(environment);
|
||||||
|
|
||||||
// 获取数据库连接配置
|
// 获取数据库连接配置
|
||||||
String dataSourceUrl = environment.getProperty("spring.datasource.url");
|
String dataSourceUrl = environment.getProperty("spring.datasource.url");
|
||||||
String dataSourceUsername = environment.getProperty("spring.datasource.username");
|
String dataSourceUsername = environment.getProperty("spring.datasource.username");
|
||||||
|
|
Loading…
Reference in New Issue