|
@@ -48,6 +48,7 @@ import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ApplicationContextAware;
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
|
+import org.springframework.context.EnvironmentAware;
|
|
import org.springframework.context.annotation.ComponentScan;
|
|
import org.springframework.context.annotation.ComponentScan;
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
import org.springframework.context.support.GenericApplicationContext;
|
|
import org.springframework.context.support.GenericApplicationContext;
|
|
@@ -79,7 +80,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|
@Slf4j
|
|
@Slf4j
|
|
@Component
|
|
@Component
|
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
-public class ApplicationContextHolder implements ApplicationContextAware, PropertyRefreshListener {
|
|
|
|
|
|
+public class ApplicationContextHolder implements EnvironmentAware, ApplicationContextAware, PropertyRefreshListener {
|
|
/**
|
|
/**
|
|
* 服务端口属性
|
|
* 服务端口属性
|
|
*/
|
|
*/
|
|
@@ -124,11 +125,21 @@ public class ApplicationContextHolder implements ApplicationContextAware, Proper
|
|
*/
|
|
*/
|
|
private static final Map<String, WeakReference<Object>> PROPERTY_OBJECT_MAPPING = Maps.newConcurrentMap();
|
|
private static final Map<String, WeakReference<Object>> PROPERTY_OBJECT_MAPPING = Maps.newConcurrentMap();
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 应用环境对象
|
|
|
|
+ */
|
|
|
|
+ private static Environment ENVIRONMENT;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 应用上下文对象
|
|
* 应用上下文对象
|
|
*/
|
|
*/
|
|
private static ApplicationContext APPLICATION_CONTEXT;
|
|
private static ApplicationContext APPLICATION_CONTEXT;
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public void setEnvironment(Environment environment) {
|
|
|
|
+ ENVIRONMENT = environment;
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
APPLICATION_CONTEXT = applicationContext;
|
|
APPLICATION_CONTEXT = applicationContext;
|
|
@@ -147,7 +158,8 @@ public class ApplicationContextHolder implements ApplicationContextAware, Proper
|
|
* @return 服务端口
|
|
* @return 服务端口
|
|
*/
|
|
*/
|
|
public static Integer getPort() {
|
|
public static Integer getPort() {
|
|
- return getPort(getApplicationContext());
|
|
|
|
|
|
+ Environment environment = getEnvironment(false);
|
|
|
|
+ return environment == null ? null : getPort(environment);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -168,11 +180,11 @@ public class ApplicationContextHolder implements ApplicationContextAware, Proper
|
|
/**
|
|
/**
|
|
* 获取服务端口
|
|
* 获取服务端口
|
|
*
|
|
*
|
|
- * @param applicationContext 应用上下文实例
|
|
|
|
|
|
+ * @param environment 应用环境
|
|
* @return 服务端口
|
|
* @return 服务端口
|
|
*/
|
|
*/
|
|
- public static Integer getPort(@NonNull ApplicationContext applicationContext) {
|
|
|
|
- String property = applicationContext.getEnvironment().getProperty(SERVER_PORT_PROPERTY);
|
|
|
|
|
|
+ public static Integer getPort(@NonNull Environment environment) {
|
|
|
|
+ String property = environment.getProperty(SERVER_PORT_PROPERTY);
|
|
return StringUtils.ifEmpty(property, Integer::parseInt);
|
|
return StringUtils.ifEmpty(property, Integer::parseInt);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -182,7 +194,8 @@ public class ApplicationContextHolder implements ApplicationContextAware, Proper
|
|
* @return 环境标识
|
|
* @return 环境标识
|
|
*/
|
|
*/
|
|
public static String getProfile() {
|
|
public static String getProfile() {
|
|
- return getProfile(getApplicationContext());
|
|
|
|
|
|
+ Environment environment = getEnvironment(false);
|
|
|
|
+ return environment == null ? null : getProfile(environment);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -199,11 +212,11 @@ public class ApplicationContextHolder implements ApplicationContextAware, Proper
|
|
/**
|
|
/**
|
|
* 获取环境标识
|
|
* 获取环境标识
|
|
*
|
|
*
|
|
- * @param applicationContext 应用上下文实例
|
|
|
|
|
|
+ * @param environment 应用环境
|
|
* @return 环境标识
|
|
* @return 环境标识
|
|
*/
|
|
*/
|
|
- public static String getProfile(@NonNull ApplicationContext applicationContext) {
|
|
|
|
- return applicationContext.getEnvironment().getProperty(APPLICATION_PROFILE_PROPERTY);
|
|
|
|
|
|
+ public static String getProfile(@NonNull Environment environment) {
|
|
|
|
+ return environment.getProperty(APPLICATION_PROFILE_PROPERTY);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -213,11 +226,7 @@ public class ApplicationContextHolder implements ApplicationContextAware, Proper
|
|
*/
|
|
*/
|
|
public static boolean isProduction() {
|
|
public static boolean isProduction() {
|
|
Boolean bool = getProperty("environment.production", Boolean.class);
|
|
Boolean bool = getProperty("environment.production", Boolean.class);
|
|
- if (bool == null) {
|
|
|
|
- ApplicationContext applicationContext = getApplicationContext(false);
|
|
|
|
- return applicationContext != null && Objects.equals(getProfile(applicationContext), "prod");
|
|
|
|
- }
|
|
|
|
- return bool;
|
|
|
|
|
|
+ return Boolean.TRUE.equals(bool) || Objects.equals(getProfile(), "prod");
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -226,7 +235,8 @@ public class ApplicationContextHolder implements ApplicationContextAware, Proper
|
|
* @return true/false
|
|
* @return true/false
|
|
*/
|
|
*/
|
|
public static boolean isMultipleServer() {
|
|
public static boolean isMultipleServer() {
|
|
- return isMultipleServer(getApplicationContext());
|
|
|
|
|
|
+ ApplicationContext applicationContext = getApplicationContext(false);
|
|
|
|
+ return applicationContext != null && isMultipleServer(applicationContext);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -251,16 +261,14 @@ public class ApplicationContextHolder implements ApplicationContextAware, Proper
|
|
/**
|
|
/**
|
|
* 获取应用环境对象
|
|
* 获取应用环境对象
|
|
*
|
|
*
|
|
- * @param required 应用环境是否必须
|
|
|
|
|
|
+ * @param required 是否必须
|
|
* @return 应用环境对象
|
|
* @return 应用环境对象
|
|
*/
|
|
*/
|
|
public static Environment getEnvironment(boolean required) {
|
|
public static Environment getEnvironment(boolean required) {
|
|
- ApplicationContext applicationContext = getApplicationContext(required);
|
|
|
|
- Environment environment = ObjectUtils.ifNull(applicationContext, ApplicationContext::getEnvironment);
|
|
|
|
if (required) {
|
|
if (required) {
|
|
- return AssertUtils.nonnull(environment, () -> "Application environment has not been initialized");
|
|
|
|
|
|
+ return AssertUtils.nonnull(ENVIRONMENT, () -> "Application environment has not been initialized");
|
|
}
|
|
}
|
|
- return environment;
|
|
|
|
|
|
+ return ENVIRONMENT;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -269,17 +277,18 @@ public class ApplicationContextHolder implements ApplicationContextAware, Proper
|
|
* @return 应用名称
|
|
* @return 应用名称
|
|
*/
|
|
*/
|
|
public static String getApplicationName() {
|
|
public static String getApplicationName() {
|
|
- return getApplicationName(getApplicationContext());
|
|
|
|
|
|
+ Environment environment = getEnvironment(false);
|
|
|
|
+ return environment == null ? null : getApplicationName(environment);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取应用名称
|
|
* 获取应用名称
|
|
*
|
|
*
|
|
- * @param applicationContext 应用上下文实例
|
|
|
|
|
|
+ * @param environment 应用环境
|
|
* @return 应用名称
|
|
* @return 应用名称
|
|
*/
|
|
*/
|
|
- public static String getApplicationName(@NonNull ApplicationContext applicationContext) {
|
|
|
|
- return applicationContext.getEnvironment().getProperty(APPLICATION_NAME_PROPERTY);
|
|
|
|
|
|
+ public static String getApplicationName(@NonNull Environment environment) {
|
|
|
|
+ return environment.getProperty(APPLICATION_NAME_PROPERTY);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -305,7 +314,7 @@ public class ApplicationContextHolder implements ApplicationContextAware, Proper
|
|
/**
|
|
/**
|
|
* 获取应用上下文对象
|
|
* 获取应用上下文对象
|
|
*
|
|
*
|
|
- * @param required 应用上下文是否必须
|
|
|
|
|
|
+ * @param required 是否必须
|
|
* @return 应用上下文对象
|
|
* @return 应用上下文对象
|
|
*/
|
|
*/
|
|
public static ApplicationContext getApplicationContext(boolean required) {
|
|
public static ApplicationContext getApplicationContext(boolean required) {
|