Commit e54448ed authored by liaozan's avatar liaozan 🏀

Refactor

parent 5c3e0dd9
package com.schbrain.common.util; package com.schbrain.common.util;
import cn.hutool.extra.spring.SpringUtil;
import com.schbrain.common.exception.BaseException;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.EnvironmentCapable;
import java.util.Optional;
/** /**
* @author liaozan * @author liaozan
...@@ -8,8 +13,15 @@ import org.springframework.core.env.Environment; ...@@ -8,8 +13,15 @@ import org.springframework.core.env.Environment;
*/ */
public class ApplicationName { public class ApplicationName {
public static String get() {
return Optional.ofNullable(SpringUtil.getApplicationContext())
.map(EnvironmentCapable::getEnvironment)
.map(ApplicationName::get)
.orElseThrow(() -> new BaseException("Could not get application name"));
}
public static String get(Environment environment) { public static String get(Environment environment) {
return environment.getRequiredProperty("spring.application.name"); return environment.getRequiredProperty("spring.application.name");
} }
} }
\ No newline at end of file
...@@ -6,14 +6,14 @@ import java.util.LinkedHashMap; ...@@ -6,14 +6,14 @@ import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
/** /**
* mark class to ensure property order * ordered property source
* *
* @author liaozan * @author liaozan
* @since 2021/12/6 * @since 2021/12/6
*/ */
public class SchbrainMapPropertySource extends MapPropertySource { public class OrderedMapPropertySource extends MapPropertySource {
public SchbrainMapPropertySource(String name, Map<String, String> source) { public OrderedMapPropertySource(String name, Map<String, String> source) {
super(name, new LinkedHashMap<>(source)); super(name, new LinkedHashMap<>(source));
} }
......
...@@ -6,7 +6,6 @@ import com.schbrain.common.exception.BaseException; ...@@ -6,7 +6,6 @@ import com.schbrain.common.exception.BaseException;
import com.schbrain.common.util.ApplicationName; import com.schbrain.common.util.ApplicationName;
import org.redisson.api.*; import org.redisson.api.*;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.util.List; import java.util.List;
...@@ -19,7 +18,7 @@ import java.util.concurrent.TimeUnit; ...@@ -19,7 +18,7 @@ import java.util.concurrent.TimeUnit;
*/ */
public class DelayedQueueUtils { public class DelayedQueueUtils {
private static final String APPLICATION_NAME = ApplicationName.get(SpringUtil.getBean(Environment.class)); private static final String APPLICATION_NAME = ApplicationName.get();
private static final ConcurrentHashMap<String, RBlockingQueue<?>> blockingQueueCache = new ConcurrentHashMap<>(); private static final ConcurrentHashMap<String, RBlockingQueue<?>> blockingQueueCache = new ConcurrentHashMap<>();
......
...@@ -8,7 +8,6 @@ import lombok.extern.slf4j.Slf4j; ...@@ -8,7 +8,6 @@ import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock; import org.redisson.api.RLock;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.core.env.Environment;
import java.time.Duration; import java.time.Duration;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
...@@ -21,7 +20,7 @@ import java.util.concurrent.TimeUnit; ...@@ -21,7 +20,7 @@ import java.util.concurrent.TimeUnit;
@Slf4j @Slf4j
public class RedisLockUtils { public class RedisLockUtils {
private static final String APPLICATION_NAME = ApplicationName.get(SpringUtil.getBean(Environment.class)); private static final String APPLICATION_NAME = ApplicationName.get();
private static RedissonClient CLIENT; private static RedissonClient CLIENT;
......
...@@ -24,8 +24,8 @@ public class BaseException extends RuntimeException { ...@@ -24,8 +24,8 @@ public class BaseException extends RuntimeException {
this(message, null); this(message, null);
} }
public BaseException(String message, Throwable throwable) { public BaseException(String message, Throwable cause) {
this(message, throwable, SERVER_ERROR, ALERT); this(message, cause, SERVER_ERROR, ALERT);
} }
public BaseException(String message, int code, int action) { public BaseException(String message, int code, int action) {
......
...@@ -242,7 +242,15 @@ public class DefaultGlobalExceptionHandler implements GlobalExceptionHandler { ...@@ -242,7 +242,15 @@ public class DefaultGlobalExceptionHandler implements GlobalExceptionHandler {
protected void logError(Throwable throwable) { protected void logError(Throwable throwable) {
String exMsg = ExceptionUtil.getMessage(throwable); String exMsg = ExceptionUtil.getMessage(throwable);
if (hasCause(throwable)) {
exMsg = exMsg + ", " + ExceptionUtil.getRootCauseMessage(throwable);
throwable = ExceptionUtil.getRootCause(throwable);
}
log.error(exMsg, throwable); log.error(exMsg, throwable);
} }
private boolean hasCause(Throwable throwable) {
return throwable.getCause() != null;
}
} }
\ No newline at end of file
...@@ -13,7 +13,6 @@ import org.aspectj.lang.reflect.MethodSignature; ...@@ -13,7 +13,6 @@ import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.BoundValueOperations; import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
...@@ -39,8 +38,8 @@ public class RateLimitAspect { ...@@ -39,8 +38,8 @@ public class RateLimitAspect {
private final StringRedisTemplate stringRedisTemplate; private final StringRedisTemplate stringRedisTemplate;
public RateLimitAspect(Environment environment, StringRedisTemplate stringRedisTemplate) { public RateLimitAspect(StringRedisTemplate stringRedisTemplate) {
this.keyPrefix = ApplicationName.get(environment); this.keyPrefix = ApplicationName.get();
this.stringRedisTemplate = stringRedisTemplate; this.stringRedisTemplate = stringRedisTemplate;
} }
......
...@@ -3,7 +3,7 @@ package com.schbrain.framework.autoconfigure.apollo; ...@@ -3,7 +3,7 @@ package com.schbrain.framework.autoconfigure.apollo;
import com.ctrip.framework.foundation.Foundation; import com.ctrip.framework.foundation.Foundation;
import com.schbrain.common.util.ApplicationName; import com.schbrain.common.util.ApplicationName;
import com.schbrain.common.util.EnvUtils; import com.schbrain.common.util.EnvUtils;
import com.schbrain.framework.support.spring.EnvironmentPostProcessorLoggerAwareAdapter; import com.schbrain.framework.support.spring.LoggerAwareEnvironmentPostProcessor;
import com.schbrain.framework.support.spring.defaults.DefaultPropertiesEnvironmentPostProcessor; import com.schbrain.framework.support.spring.defaults.DefaultPropertiesEnvironmentPostProcessor;
import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
...@@ -23,7 +23,7 @@ import static com.ctrip.framework.apollo.spring.config.PropertySourcesConstants. ...@@ -23,7 +23,7 @@ import static com.ctrip.framework.apollo.spring.config.PropertySourcesConstants.
* @author liaozan * @author liaozan
* @since 2021/11/6 * @since 2021/11/6
*/ */
public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLoggerAwareAdapter implements Ordered { public class ApolloConfigurationInitializerEnvironmentPostProcessor extends LoggerAwareEnvironmentPostProcessor implements Ordered {
/** /**
* load properties after set the default properties * load properties after set the default properties
...@@ -36,9 +36,9 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg ...@@ -36,9 +36,9 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg
private final ConfigurablePropertiesLoader configurablePropertiesLoader; private final ConfigurablePropertiesLoader configurablePropertiesLoader;
public ApolloConfigurationInitializer(DeferredLogFactory deferredLogFactory, ConfigurableBootstrapContext bootstrapContext) { public ApolloConfigurationInitializerEnvironmentPostProcessor(DeferredLogFactory deferredLogFactory, ConfigurableBootstrapContext bootstrapContext) {
super(deferredLogFactory, bootstrapContext); super(deferredLogFactory, bootstrapContext);
this.configurablePropertiesLoader = new ConfigurablePropertiesLoader(getDeferredLogFactory()); this.configurablePropertiesLoader = new ConfigurablePropertiesLoader(deferredLogFactory);
} }
@Override @Override
...@@ -63,26 +63,26 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg ...@@ -63,26 +63,26 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg
private void setRequiredProperty(ConfigurableEnvironment environment) { private void setRequiredProperty(ConfigurableEnvironment environment) {
String appId = getAppId(environment); String appId = getAppId(environment);
saveProperty(APP_ID, appId); setPropertyToSystem(APP_ID, appId);
String env = getEnv(environment); String env = getEnv(environment);
saveProperty(ENV_KEY, env); setPropertyToSystem(ENV_KEY, env);
String apolloUrl = getApolloUrl(environment, env); String metaServerUrl = getApolloMetaServerUrl(environment, env);
saveProperty(APOLLO_META, apolloUrl); setPropertyToSystem(APOLLO_META, metaServerUrl);
saveProperty(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, true); setPropertyToSystem(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, true);
saveProperty(APOLLO_BOOTSTRAP_ENABLED, true); setPropertyToSystem(APOLLO_BOOTSTRAP_ENABLED, true);
saveProperty(APOLLO_CACHE_FILE_ENABLE, true); setPropertyToSystem(APOLLO_CACHE_FILE_ENABLE, true);
saveProperty(APOLLO_PROPERTY_ORDER_ENABLE, true); setPropertyToSystem(APOLLO_PROPERTY_ORDER_ENABLE, true);
// DO NOT set to true. After caching the property name, SpringBoot may not be able to bind the properties // DO NOT set to true. After caching the property name, SpringBoot may not be able to bind the properties
saveProperty(APOLLO_PROPERTY_NAMES_CACHE_ENABLE, false); setPropertyToSystem(APOLLO_PROPERTY_NAMES_CACHE_ENABLE, false);
saveProperty(APOLLO_OVERRIDE_SYSTEM_PROPERTIES, false); setPropertyToSystem(APOLLO_OVERRIDE_SYSTEM_PROPERTIES, false);
printProperties(); printProperties();
} }
private void saveProperty(String key, Object value) { private void setPropertyToSystem(String key, Object value) {
INIT_PROPERTIES.put(key, value); INIT_PROPERTIES.put(key, value);
System.setProperty(key, value.toString()); System.setProperty(key, value.toString());
} }
...@@ -97,7 +97,12 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg ...@@ -97,7 +97,12 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg
System.out.println(message); System.out.println(message);
} }
private String getApolloUrl(ConfigurableEnvironment environment, String env) { /**
* get apollo meta server url
*
* @see com.ctrip.framework.foundation.internals.provider.DefaultApplicationProvider#getProperty(String, String)
*/
private String getApolloMetaServerUrl(ConfigurableEnvironment environment, String env) {
String fallbackKey = env + ".meta"; String fallbackKey = env + ".meta";
// {env}.meta // {env}.meta
...@@ -125,6 +130,11 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg ...@@ -125,6 +130,11 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg
return Foundation.getProperty(fallbackKey, null); return Foundation.getProperty(fallbackKey, null);
} }
/**
* get env
*
* @see #getApolloMetaServerUrl(org.springframework.core.env.ConfigurableEnvironment, String)
*/
private String getEnv(ConfigurableEnvironment environment) { private String getEnv(ConfigurableEnvironment environment) {
String profile = EnvUtils.getProfile(environment); String profile = EnvUtils.getProfile(environment);
if (profile == null) { if (profile == null) {
...@@ -133,6 +143,12 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg ...@@ -133,6 +143,12 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg
return profile; return profile;
} }
/**
* get AppId
*
* @see com.ctrip.framework.foundation.internals.provider.DefaultApplicationProvider#initAppId()
*/
@SuppressWarnings("JavadocReference")
private String getAppId(ConfigurableEnvironment environment) { private String getAppId(ConfigurableEnvironment environment) {
String appId; String appId;
if (environment.containsProperty(APP_ID)) { if (environment.containsProperty(APP_ID)) {
......
...@@ -3,7 +3,7 @@ package com.schbrain.framework.autoconfigure.apollo; ...@@ -3,7 +3,7 @@ package com.schbrain.framework.autoconfigure.apollo;
import cn.hutool.core.thread.GlobalThreadPool; import cn.hutool.core.thread.GlobalThreadPool;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService; import com.ctrip.framework.apollo.ConfigService;
import com.schbrain.common.util.properties.SchbrainMapPropertySource; import com.schbrain.common.util.properties.OrderedMapPropertySource;
import com.schbrain.common.util.support.ConfigurableProperties; import com.schbrain.common.util.support.ConfigurableProperties;
import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEvent; import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEvent;
import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEventListener; import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEventListener;
...@@ -66,7 +66,7 @@ class ConfigurablePropertiesLoader { ...@@ -66,7 +66,7 @@ class ConfigurablePropertiesLoader {
configurableProperties.forEach(properties -> { configurableProperties.forEach(properties -> {
String namespace = properties.getDefaultNamespace(); String namespace = properties.getDefaultNamespace();
Config config = ConfigService.getConfig(namespace); Config config = ConfigService.getConfig(namespace);
SchbrainMapPropertySource propertySource = ConfigUtils.toPropertySource(namespace, config); OrderedMapPropertySource propertySource = ConfigUtils.toPropertySource(namespace, config);
if (propertySource == null) { if (propertySource == null) {
log.warn("No configuration properties loaded under namespace: " + namespace); log.warn("No configuration properties loaded under namespace: " + namespace);
return; return;
......
package com.schbrain.framework.autoconfigure.apollo.listener; package com.schbrain.framework.autoconfigure.apollo.listener;
import com.schbrain.common.util.properties.SchbrainMapPropertySource; import com.schbrain.common.util.properties.OrderedMapPropertySource;
import com.schbrain.common.util.support.ConfigurableProperties; import com.schbrain.common.util.support.ConfigurableProperties;
import lombok.Getter; import lombok.Getter;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
...@@ -21,13 +21,13 @@ public class PropertiesPreparedEvent extends ApplicationEvent { ...@@ -21,13 +21,13 @@ public class PropertiesPreparedEvent extends ApplicationEvent {
private final DeferredLogFactory deferredLogFactory; private final DeferredLogFactory deferredLogFactory;
private final SchbrainMapPropertySource propertySource; private final OrderedMapPropertySource propertySource;
private final SpringApplication application; private final SpringApplication application;
public PropertiesPreparedEvent(ConfigurableEnvironment environment, public PropertiesPreparedEvent(ConfigurableEnvironment environment,
DeferredLogFactory deferredLogFactory, DeferredLogFactory deferredLogFactory,
SchbrainMapPropertySource propertySource, OrderedMapPropertySource propertySource,
ConfigurableProperties properties, ConfigurableProperties properties,
SpringApplication application) { SpringApplication application) {
super(properties); super(properties);
......
...@@ -2,7 +2,7 @@ package com.schbrain.framework.autoconfigure.apollo.util; ...@@ -2,7 +2,7 @@ package com.schbrain.framework.autoconfigure.apollo.util;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.Config;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.schbrain.common.util.properties.SchbrainMapPropertySource; import com.schbrain.common.util.properties.OrderedMapPropertySource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
...@@ -19,7 +19,7 @@ import java.util.Set; ...@@ -19,7 +19,7 @@ import java.util.Set;
public class ConfigUtils { public class ConfigUtils {
@Nullable @Nullable
public static SchbrainMapPropertySource toPropertySource(String name, Config config) { public static OrderedMapPropertySource toPropertySource(String name, Config config) {
Set<String> propertyNames = config.getPropertyNames(); Set<String> propertyNames = config.getPropertyNames();
if (propertyNames.isEmpty()) { if (propertyNames.isEmpty()) {
return null; return null;
...@@ -29,10 +29,10 @@ public class ConfigUtils { ...@@ -29,10 +29,10 @@ public class ConfigUtils {
String property = config.getProperty(propertyName, null); String property = config.getProperty(propertyName, null);
configs.put(propertyName, property); configs.put(propertyName, property);
} }
return new SchbrainMapPropertySource(name, configs); return new OrderedMapPropertySource(name, configs);
} }
public static void resolvePlaceHolders(ConfigurableEnvironment environment, SchbrainMapPropertySource propertySource) { public static void resolvePlaceHolders(ConfigurableEnvironment environment, OrderedMapPropertySource propertySource) {
Map<String, Object> source = propertySource.getSource(); Map<String, Object> source = propertySource.getSource();
for (Entry<String, Object> entry : source.entrySet()) { for (Entry<String, Object> entry : source.entrySet()) {
Object value = entry.getValue(); Object value = entry.getValue();
......
org.springframework.boot.env.EnvironmentPostProcessor=\ org.springframework.boot.env.EnvironmentPostProcessor=\
com.schbrain.framework.autoconfigure.apollo.ApolloConfigurationInitializer,\ com.schbrain.framework.autoconfigure.apollo.ApolloConfigurationInitializerEnvironmentPostProcessor,\
com.schbrain.framework.autoconfigure.apollo.ApolloPropertiesReorderEnvironmentPostProcessor com.schbrain.framework.autoconfigure.apollo.ApolloPropertiesReorderEnvironmentPostProcessor
\ No newline at end of file
...@@ -129,6 +129,9 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte ...@@ -129,6 +129,9 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
if (this.bizIdColumnField != null) { if (this.bizIdColumnField != null) {
throw new BaseException(String.format("@BizId can't more than one in Class: \"%s\"", entityClass.getName())); throw new BaseException(String.format("@BizId can't more than one in Class: \"%s\"", entityClass.getName()));
} }
if (bizId.getType() != String.class) {
throw new BaseException("@BizId only support String field");
}
this.bizIdColumnField = new BizIdColumnField(entityClass, bizId); this.bizIdColumnField = new BizIdColumnField(entityClass, bizId);
BizIdHelper.putBizColumnField(entityClass, bizIdColumnField); BizIdHelper.putBizColumnField(entityClass, bizIdColumnField);
}, field -> field.isAnnotationPresent(BizId.class)); }, field -> field.isAnnotationPresent(BizId.class));
......
...@@ -7,8 +7,6 @@ import org.apache.ibatis.executor.Executor; ...@@ -7,8 +7,6 @@ import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.mapping.SqlCommandType;
import java.sql.SQLException;
/** /**
* @author liaozan * @author liaozan
* @since 2023-04-17 * @since 2023-04-17
...@@ -16,13 +14,12 @@ import java.sql.SQLException; ...@@ -16,13 +14,12 @@ import java.sql.SQLException;
public class BizIdInjectInterceptor implements InnerInterceptor { public class BizIdInjectInterceptor implements InnerInterceptor {
@Override @Override
public void beforeUpdate(Executor executor, MappedStatement ms, Object entity) throws SQLException { public void beforeUpdate(Executor executor, MappedStatement ms, Object entity) {
SqlCommandType sqlCommandType = ms.getSqlCommandType(); SqlCommandType sqlCommandType = ms.getSqlCommandType();
if (sqlCommandType != SqlCommandType.INSERT) { if (sqlCommandType != SqlCommandType.INSERT) {
return; return;
} }
Class<?> entityClass = entity.getClass(); BizIdColumnField bizColumnField = BizIdHelper.getBizColumnField(entity.getClass());
BizIdColumnField bizColumnField = BizIdHelper.getBizColumnField(entityClass);
if (bizColumnField == null) { if (bizColumnField == null) {
return; return;
} }
......
...@@ -4,8 +4,6 @@ import com.schbrain.framework.autoconfigure.oss.properties.OssProperties; ...@@ -4,8 +4,6 @@ import com.schbrain.framework.autoconfigure.oss.properties.OssProperties;
import com.schbrain.framework.autoconfigure.oss.util.OssUtils; import com.schbrain.framework.autoconfigure.oss.util.OssUtils;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
/** /**
* @author liaozan * @author liaozan
...@@ -15,9 +13,8 @@ import org.springframework.core.env.ConfigurableEnvironment; ...@@ -15,9 +13,8 @@ import org.springframework.core.env.ConfigurableEnvironment;
@EnableConfigurationProperties(OssProperties.class) @EnableConfigurationProperties(OssProperties.class)
public class OssAutoConfiguration { public class OssAutoConfiguration {
public OssAutoConfiguration(ConfigurableApplicationContext applicationContext, OssProperties ossProperties) { public OssAutoConfiguration(OssProperties ossProperties) {
ConfigurableEnvironment environment = applicationContext.getEnvironment(); OssUtils.initialize(ossProperties);
OssUtils.initialize(environment, ossProperties);
} }
} }
\ No newline at end of file
...@@ -22,7 +22,6 @@ import com.schbrain.framework.autoconfigure.oss.exception.OssException; ...@@ -22,7 +22,6 @@ import com.schbrain.framework.autoconfigure.oss.exception.OssException;
import com.schbrain.framework.autoconfigure.oss.properties.OssProperties; import com.schbrain.framework.autoconfigure.oss.properties.OssProperties;
import com.schbrain.framework.autoconfigure.oss.properties.OssProperties.StsProperties; import com.schbrain.framework.autoconfigure.oss.properties.OssProperties.StsProperties;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.io.File; import java.io.File;
...@@ -48,7 +47,7 @@ public class OssUtils { ...@@ -48,7 +47,7 @@ public class OssUtils {
private static StsProperties stsProperties; private static StsProperties stsProperties;
private static String directory; private static String directory;
public static void initialize(ConfigurableEnvironment environment, OssProperties properties) { public static void initialize(OssProperties properties) {
if (properties == null || properties.isInValid()) { if (properties == null || properties.isInValid()) {
log.warn("ossProperties is invalid, OssUtils will not available until reinitialize with the correct configuration"); log.warn("ossProperties is invalid, OssUtils will not available until reinitialize with the correct configuration");
return; return;
...@@ -60,7 +59,7 @@ public class OssUtils { ...@@ -60,7 +59,7 @@ public class OssUtils {
stsProperties = properties.getSts(); stsProperties = properties.getSts();
directory = properties.getDirectory(); directory = properties.getDirectory();
if (directory == null) { if (directory == null) {
directory = ApplicationName.get(environment); directory = ApplicationName.get();
} }
} catch (Exception e) { } catch (Exception e) {
log.warn("oss initialize fail, OssUtils will not available until reinitialize with the correct configuration", e); log.warn("oss initialize fail, OssUtils will not available until reinitialize with the correct configuration", e);
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
<groupId>com.schbrain.framework</groupId> <groupId>com.schbrain.framework</groupId>
<artifactId>schbrain-parent</artifactId> <artifactId>schbrain-parent</artifactId>
<version>${revision}</version> <version>${revision}</version>
</parent> </parent>
<artifactId>starters</artifactId> <artifactId>starters</artifactId>
......
...@@ -10,7 +10,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean ...@@ -10,7 +10,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.core.env.ConfigurableEnvironment;
import java.nio.file.Paths; import java.nio.file.Paths;
...@@ -25,15 +24,12 @@ public class XxlJobAutoConfiguration { ...@@ -25,15 +24,12 @@ public class XxlJobAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(XxlJobExecutor.class) @ConditionalOnMissingBean(XxlJobExecutor.class)
public SchbrainXxlJobExecutor schbrainXxlJobSpringExecutor(ConfigurableEnvironment environment, public SchbrainXxlJobExecutor schbrainXxlJobSpringExecutor(XxlJobProperties xxlJobProperties, LoggerProperties loggingProperties) {
XxlJobProperties xxlJobProperties,
LoggerProperties loggingProperties) {
String applicationName = ApplicationName.get(environment);
SchbrainXxlJobExecutor executor = new SchbrainXxlJobExecutor(); SchbrainXxlJobExecutor executor = new SchbrainXxlJobExecutor();
executor.setAdminAddresses(xxlJobProperties.getAdminAddresses()); executor.setAdminAddresses(xxlJobProperties.getAdminAddresses());
executor.setIp(xxlJobProperties.getIp()); executor.setIp(xxlJobProperties.getIp());
executor.setPort(xxlJobProperties.getPort()); executor.setPort(xxlJobProperties.getPort());
executor.setAppName(applicationName); executor.setAppName(ApplicationName.get());
executor.setAccessToken(xxlJobProperties.getAccessToken()); executor.setAccessToken(xxlJobProperties.getAccessToken());
executor.setLogPath(Paths.get(loggingProperties.getLogPath(), "xxl-job").toString()); executor.setLogPath(Paths.get(loggingProperties.getLogPath(), "xxl-job").toString());
executor.setLogRetentionDays(xxlJobProperties.getLogRetentionDays()); executor.setLogRetentionDays(xxlJobProperties.getLogRetentionDays());
......
...@@ -2,25 +2,32 @@ package com.schbrain.framework.support.spring; ...@@ -2,25 +2,32 @@ package com.schbrain.framework.support.spring;
import lombok.Getter; import lombok.Getter;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.springframework.boot.BootstrapContext;
import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.context.ConfigurableApplicationContext;
/** /**
* @author liaozan * @author liaozan
* @since 2021/11/22 * @since 2021/11/22
*/ */
@Getter @Getter
public abstract class EnvironmentPostProcessorLoggerAwareAdapter implements EnvironmentPostProcessor { public abstract class LoggerAwareEnvironmentPostProcessor implements EnvironmentPostProcessor {
private final Log log; protected final Log log;
private final DeferredLogFactory deferredLogFactory; protected final DeferredLogFactory deferredLogFactory;
private final ConfigurableBootstrapContext bootstrapContext; protected final ConfigurableBootstrapContext bootstrapContext;
public EnvironmentPostProcessorLoggerAwareAdapter(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) { public LoggerAwareEnvironmentPostProcessor(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) {
this.log = logFactory.getLog(getClass()); this.log = logFactory.getLog(getClass());
this.bootstrapContext = bootstrapContext; this.bootstrapContext = bootstrapContext;
this.deferredLogFactory = logFactory; this.deferredLogFactory = logFactory;
this.bootstrapContext.addCloseListener(event -> onBootstrapContextClose(event.getBootstrapContext(), event.getApplicationContext()));
}
protected void onBootstrapContextClose(BootstrapContext bootstrapContext, ConfigurableApplicationContext applicationContext) {
} }
} }
\ No newline at end of file
...@@ -5,7 +5,7 @@ import cn.hutool.core.util.ArrayUtil; ...@@ -5,7 +5,7 @@ import cn.hutool.core.util.ArrayUtil;
import com.schbrain.common.constants.DateTimeFormatters; import com.schbrain.common.constants.DateTimeFormatters;
import com.schbrain.common.util.EnvUtils; import com.schbrain.common.util.EnvUtils;
import com.schbrain.common.util.PortUtils; import com.schbrain.common.util.PortUtils;
import com.schbrain.framework.support.spring.EnvironmentPostProcessorLoggerAwareAdapter; import com.schbrain.framework.support.spring.LoggerAwareEnvironmentPostProcessor;
import org.springframework.boot.*; import org.springframework.boot.*;
import org.springframework.boot.actuate.autoconfigure.health.HealthProperties.Show; import org.springframework.boot.actuate.autoconfigure.health.HealthProperties.Show;
import org.springframework.boot.actuate.info.InfoPropertiesInfoContributor.Mode; import org.springframework.boot.actuate.info.InfoPropertiesInfoContributor.Mode;
...@@ -23,7 +23,7 @@ import java.util.*; ...@@ -23,7 +23,7 @@ import java.util.*;
* @author liaozan * @author liaozan
* @since 2021/12/18 * @since 2021/12/18
*/ */
public class DefaultPropertiesEnvironmentPostProcessor extends EnvironmentPostProcessorLoggerAwareAdapter implements Ordered { public class DefaultPropertiesEnvironmentPostProcessor extends LoggerAwareEnvironmentPostProcessor implements Ordered {
/** /**
* set default properties after configData loaded * set default properties after configData loaded
...@@ -80,7 +80,7 @@ public class DefaultPropertiesEnvironmentPostProcessor extends EnvironmentPostPr ...@@ -80,7 +80,7 @@ public class DefaultPropertiesEnvironmentPostProcessor extends EnvironmentPostPr
if (ArrayUtil.isEmpty(environment.getActiveProfiles())) { if (ArrayUtil.isEmpty(environment.getActiveProfiles())) {
environment.setActiveProfiles(EnvUtils.DEVELOPMENT); environment.setActiveProfiles(EnvUtils.DEVELOPMENT);
defaultProperties.put(SPRING_PROFILE_ACTIVE, EnvUtils.DEVELOPMENT); defaultProperties.put(SPRING_PROFILE_ACTIVE, EnvUtils.DEVELOPMENT);
getLog().info(StrFormatter.format("{} is unset, set to {} by default", SPRING_PROFILE_ACTIVE, EnvUtils.DEVELOPMENT)); log.info(StrFormatter.format("{} is unset, set to {} by default", SPRING_PROFILE_ACTIVE, EnvUtils.DEVELOPMENT));
} }
} }
...@@ -92,8 +92,8 @@ public class DefaultPropertiesEnvironmentPostProcessor extends EnvironmentPostPr ...@@ -92,8 +92,8 @@ public class DefaultPropertiesEnvironmentPostProcessor extends EnvironmentPostPr
return; return;
} }
if (!environment.containsProperty(DUBBO_REGISTER_KEY)) { if (!environment.containsProperty(DUBBO_REGISTER_KEY)) {
getLog().info(StrFormatter.format("Not running on CloudPlatform, {} is set to false by default", DUBBO_REGISTER_KEY)); log.info(StrFormatter.format("Not running on CloudPlatform, {} is set to false by default", DUBBO_REGISTER_KEY));
getLog().info(StrFormatter.format("If you want force to register with Dubbo Registry, set {} = true", DUBBO_REGISTER_KEY)); log.info(StrFormatter.format("If you want force to register with Dubbo Registry, set {} = true", DUBBO_REGISTER_KEY));
defaultProperties.put(DUBBO_REGISTER_KEY, false); defaultProperties.put(DUBBO_REGISTER_KEY, false);
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment