diff --git a/commons/common-util/src/main/java/com/schbrain/common/util/ApplicationName.java b/commons/common-util/src/main/java/com/schbrain/common/util/ApplicationName.java index e4ec354963f4a22aa2e340170b2f7bc5fc085601..4c98211be884a2563a03fd11fc42a152aca3246b 100644 --- a/commons/common-util/src/main/java/com/schbrain/common/util/ApplicationName.java +++ b/commons/common-util/src/main/java/com/schbrain/common/util/ApplicationName.java @@ -1,6 +1,11 @@ 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.EnvironmentCapable; + +import java.util.Optional; /** * @author liaozan @@ -8,8 +13,15 @@ import org.springframework.core.env.Environment; */ 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) { return environment.getRequiredProperty("spring.application.name"); } -} +} \ No newline at end of file diff --git a/commons/common-util/src/main/java/com/schbrain/common/util/properties/SchbrainMapPropertySource.java b/commons/common-util/src/main/java/com/schbrain/common/util/properties/OrderedMapPropertySource.java similarity index 66% rename from commons/common-util/src/main/java/com/schbrain/common/util/properties/SchbrainMapPropertySource.java rename to commons/common-util/src/main/java/com/schbrain/common/util/properties/OrderedMapPropertySource.java index 072c252b2c0531200a606ed0ec37665494805b57..7ba824594d2a376c5bd8bf7c88ef512f538812f9 100644 --- a/commons/common-util/src/main/java/com/schbrain/common/util/properties/SchbrainMapPropertySource.java +++ b/commons/common-util/src/main/java/com/schbrain/common/util/properties/OrderedMapPropertySource.java @@ -6,14 +6,14 @@ import java.util.LinkedHashMap; import java.util.Map; /** - * mark class to ensure property order + * ordered property source * * @author liaozan * @since 2021/12/6 */ -public class SchbrainMapPropertySource extends MapPropertySource { +public class OrderedMapPropertySource extends MapPropertySource { - public SchbrainMapPropertySource(String name, Map source) { + public OrderedMapPropertySource(String name, Map source) { super(name, new LinkedHashMap<>(source)); } diff --git a/commons/common-util/src/main/java/com/schbrain/common/util/support/delay/DelayedQueueUtils.java b/commons/common-util/src/main/java/com/schbrain/common/util/support/delay/DelayedQueueUtils.java index 01b964536ce5db5c438b6c5b2f9d0e79fd683d38..2d0a6b0c3237a2763f7da51e9822be101466ec1e 100644 --- a/commons/common-util/src/main/java/com/schbrain/common/util/support/delay/DelayedQueueUtils.java +++ b/commons/common-util/src/main/java/com/schbrain/common/util/support/delay/DelayedQueueUtils.java @@ -6,7 +6,6 @@ import com.schbrain.common.exception.BaseException; import com.schbrain.common.util.ApplicationName; import org.redisson.api.*; import org.springframework.beans.BeansException; -import org.springframework.core.env.Environment; import org.springframework.util.CollectionUtils; import java.util.List; @@ -19,7 +18,7 @@ import java.util.concurrent.TimeUnit; */ 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> blockingQueueCache = new ConcurrentHashMap<>(); diff --git a/commons/common-util/src/main/java/com/schbrain/common/util/support/lock/RedisLockUtils.java b/commons/common-util/src/main/java/com/schbrain/common/util/support/lock/RedisLockUtils.java index 8c9826712353e82c1f95da57f81e86c9e526d303..81d140d7c19769868d67ac1f2331f0c5d5f18470 100644 --- a/commons/common-util/src/main/java/com/schbrain/common/util/support/lock/RedisLockUtils.java +++ b/commons/common-util/src/main/java/com/schbrain/common/util/support/lock/RedisLockUtils.java @@ -8,7 +8,6 @@ import lombok.extern.slf4j.Slf4j; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.springframework.beans.BeansException; -import org.springframework.core.env.Environment; import java.time.Duration; import java.util.concurrent.Callable; @@ -21,7 +20,7 @@ import java.util.concurrent.TimeUnit; @Slf4j 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; diff --git a/commons/common/src/main/java/com/schbrain/common/exception/BaseException.java b/commons/common/src/main/java/com/schbrain/common/exception/BaseException.java index 4692ef7a9fed0fab01511fb164145b2f583da37d..6a7bb5996b1358e2d0b92c550c710d4d98deb548 100644 --- a/commons/common/src/main/java/com/schbrain/common/exception/BaseException.java +++ b/commons/common/src/main/java/com/schbrain/common/exception/BaseException.java @@ -24,8 +24,8 @@ public class BaseException extends RuntimeException { this(message, null); } - public BaseException(String message, Throwable throwable) { - this(message, throwable, SERVER_ERROR, ALERT); + public BaseException(String message, Throwable cause) { + this(message, cause, SERVER_ERROR, ALERT); } public BaseException(String message, int code, int action) { diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionHandler.java b/commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionHandler.java index 7fe112104449865692183d4f6ab9dbbc521e1167..9967a99729323307b22e7615730ed45c7be94190 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionHandler.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionHandler.java @@ -242,7 +242,15 @@ public class DefaultGlobalExceptionHandler implements GlobalExceptionHandler { protected void logError(Throwable throwable) { String exMsg = ExceptionUtil.getMessage(throwable); + if (hasCause(throwable)) { + exMsg = exMsg + ", " + ExceptionUtil.getRootCauseMessage(throwable); + throwable = ExceptionUtil.getRootCause(throwable); + } log.error(exMsg, throwable); } + private boolean hasCause(Throwable throwable) { + return throwable.getCause() != null; + } + } \ No newline at end of file diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/support/concurrent/RateLimitAspect.java b/commons/web-common/src/main/java/com/schbrain/common/web/support/concurrent/RateLimitAspect.java index e3c9ef8528c0e55ff4177c60faf874d92a42216d..6763c4ac686c23a100ec876fe5b3d80bb51e7390 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/support/concurrent/RateLimitAspect.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/support/concurrent/RateLimitAspect.java @@ -13,7 +13,6 @@ import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.BeanUtils; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 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.core.BoundValueOperations; import org.springframework.data.redis.core.StringRedisTemplate; @@ -39,8 +38,8 @@ public class RateLimitAspect { private final StringRedisTemplate stringRedisTemplate; - public RateLimitAspect(Environment environment, StringRedisTemplate stringRedisTemplate) { - this.keyPrefix = ApplicationName.get(environment); + public RateLimitAspect(StringRedisTemplate stringRedisTemplate) { + this.keyPrefix = ApplicationName.get(); this.stringRedisTemplate = stringRedisTemplate; } diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloConfigurationInitializer.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloConfigurationInitializerEnvironmentPostProcessor.java similarity index 71% rename from starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloConfigurationInitializer.java rename to starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloConfigurationInitializerEnvironmentPostProcessor.java index a9ed19406a57f4cc3a57e22751962d5d96714152..764ea03032a01171ac39b294a7ee68e63e28692d 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloConfigurationInitializer.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloConfigurationInitializerEnvironmentPostProcessor.java @@ -3,7 +3,7 @@ package com.schbrain.framework.autoconfigure.apollo; import com.ctrip.framework.foundation.Foundation; import com.schbrain.common.util.ApplicationName; 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 org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.SpringApplication; @@ -23,7 +23,7 @@ import static com.ctrip.framework.apollo.spring.config.PropertySourcesConstants. * @author liaozan * @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 @@ -36,9 +36,9 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg private final ConfigurablePropertiesLoader configurablePropertiesLoader; - public ApolloConfigurationInitializer(DeferredLogFactory deferredLogFactory, ConfigurableBootstrapContext bootstrapContext) { + public ApolloConfigurationInitializerEnvironmentPostProcessor(DeferredLogFactory deferredLogFactory, ConfigurableBootstrapContext bootstrapContext) { super(deferredLogFactory, bootstrapContext); - this.configurablePropertiesLoader = new ConfigurablePropertiesLoader(getDeferredLogFactory()); + this.configurablePropertiesLoader = new ConfigurablePropertiesLoader(deferredLogFactory); } @Override @@ -63,26 +63,26 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg private void setRequiredProperty(ConfigurableEnvironment environment) { String appId = getAppId(environment); - saveProperty(APP_ID, appId); + setPropertyToSystem(APP_ID, appId); String env = getEnv(environment); - saveProperty(ENV_KEY, env); + setPropertyToSystem(ENV_KEY, env); - String apolloUrl = getApolloUrl(environment, env); - saveProperty(APOLLO_META, apolloUrl); + String metaServerUrl = getApolloMetaServerUrl(environment, env); + setPropertyToSystem(APOLLO_META, metaServerUrl); - saveProperty(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, true); - saveProperty(APOLLO_BOOTSTRAP_ENABLED, true); - saveProperty(APOLLO_CACHE_FILE_ENABLE, true); - saveProperty(APOLLO_PROPERTY_ORDER_ENABLE, true); + setPropertyToSystem(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, true); + setPropertyToSystem(APOLLO_BOOTSTRAP_ENABLED, true); + setPropertyToSystem(APOLLO_CACHE_FILE_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 - saveProperty(APOLLO_PROPERTY_NAMES_CACHE_ENABLE, false); - saveProperty(APOLLO_OVERRIDE_SYSTEM_PROPERTIES, false); + setPropertyToSystem(APOLLO_PROPERTY_NAMES_CACHE_ENABLE, false); + setPropertyToSystem(APOLLO_OVERRIDE_SYSTEM_PROPERTIES, false); printProperties(); } - private void saveProperty(String key, Object value) { + private void setPropertyToSystem(String key, Object value) { INIT_PROPERTIES.put(key, value); System.setProperty(key, value.toString()); } @@ -97,7 +97,12 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg 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"; // {env}.meta @@ -125,6 +130,11 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg return Foundation.getProperty(fallbackKey, null); } + /** + * get env + * + * @see #getApolloMetaServerUrl(org.springframework.core.env.ConfigurableEnvironment, String) + */ private String getEnv(ConfigurableEnvironment environment) { String profile = EnvUtils.getProfile(environment); if (profile == null) { @@ -133,6 +143,12 @@ public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLogg return profile; } + /** + * get AppId + * + * @see com.ctrip.framework.foundation.internals.provider.DefaultApplicationProvider#initAppId() + */ + @SuppressWarnings("JavadocReference") private String getAppId(ConfigurableEnvironment environment) { String appId; if (environment.containsProperty(APP_ID)) { diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java index 1069ae057f7254b6ff92f8a43bb50c2dfa5941f4..4e78bafb8751b3da964ad8c3ec492fb28bb86cbf 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java @@ -3,7 +3,7 @@ package com.schbrain.framework.autoconfigure.apollo; import cn.hutool.core.thread.GlobalThreadPool; import com.ctrip.framework.apollo.Config; 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.framework.autoconfigure.apollo.listener.PropertiesPreparedEvent; import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEventListener; @@ -66,7 +66,7 @@ class ConfigurablePropertiesLoader { configurableProperties.forEach(properties -> { String namespace = properties.getDefaultNamespace(); Config config = ConfigService.getConfig(namespace); - SchbrainMapPropertySource propertySource = ConfigUtils.toPropertySource(namespace, config); + OrderedMapPropertySource propertySource = ConfigUtils.toPropertySource(namespace, config); if (propertySource == null) { log.warn("No configuration properties loaded under namespace: " + namespace); return; diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/listener/PropertiesPreparedEvent.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/listener/PropertiesPreparedEvent.java index 60206e295b89c563a4ff8297ca009e42ede53a43..b422726affd3e494b707152ee8608f45d9c2cfd0 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/listener/PropertiesPreparedEvent.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/listener/PropertiesPreparedEvent.java @@ -1,6 +1,6 @@ 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 lombok.Getter; import org.springframework.boot.SpringApplication; @@ -21,13 +21,13 @@ public class PropertiesPreparedEvent extends ApplicationEvent { private final DeferredLogFactory deferredLogFactory; - private final SchbrainMapPropertySource propertySource; + private final OrderedMapPropertySource propertySource; private final SpringApplication application; public PropertiesPreparedEvent(ConfigurableEnvironment environment, DeferredLogFactory deferredLogFactory, - SchbrainMapPropertySource propertySource, + OrderedMapPropertySource propertySource, ConfigurableProperties properties, SpringApplication application) { super(properties); diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/util/ConfigUtils.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/util/ConfigUtils.java index d77f0c52e05936606b1cd56e387dee3144fb1bc1..92e871f7d8f94eaa9aa83bb813c9265ad0f55ae9 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/util/ConfigUtils.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/util/ConfigUtils.java @@ -2,7 +2,7 @@ package com.schbrain.framework.autoconfigure.apollo.util; import com.ctrip.framework.apollo.Config; 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 org.springframework.core.env.ConfigurableEnvironment; @@ -19,7 +19,7 @@ import java.util.Set; public class ConfigUtils { @Nullable - public static SchbrainMapPropertySource toPropertySource(String name, Config config) { + public static OrderedMapPropertySource toPropertySource(String name, Config config) { Set propertyNames = config.getPropertyNames(); if (propertyNames.isEmpty()) { return null; @@ -29,10 +29,10 @@ public class ConfigUtils { String property = config.getProperty(propertyName, null); 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 source = propertySource.getSource(); for (Entry entry : source.entrySet()) { Object value = entry.getValue(); diff --git a/starters/apollo-spring-boot-starter/src/main/resources/META-INF/spring.factories b/starters/apollo-spring-boot-starter/src/main/resources/META-INF/spring.factories index 955c229eaef25503e95ec8f4dda39019f3770f7c..a704ada365e2a0799660c5585d6099b3773f28c8 100644 --- a/starters/apollo-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/starters/apollo-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -1,3 +1,3 @@ org.springframework.boot.env.EnvironmentPostProcessor=\ - com.schbrain.framework.autoconfigure.apollo.ApolloConfigurationInitializer,\ + com.schbrain.framework.autoconfigure.apollo.ApolloConfigurationInitializerEnvironmentPostProcessor,\ com.schbrain.framework.autoconfigure.apollo.ApolloPropertiesReorderEnvironmentPostProcessor \ No newline at end of file diff --git a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java index 276018a296a0d66e58a6c4bdd2ee22740b967f74..95e3d3a2aa3ed8b1d13367eacab1cdae95eba412 100644 --- a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java +++ b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java @@ -129,6 +129,9 @@ public class BaseServiceImpl, T extends BaseEntity> exte if (this.bizIdColumnField != null) { 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); BizIdHelper.putBizColumnField(entityClass, bizIdColumnField); }, field -> field.isAnnotationPresent(BizId.class)); diff --git a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/biz/BizIdInjectInterceptor.java b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/biz/BizIdInjectInterceptor.java index 08d80b13e8dd0d22385edb35785080a4af323c5b..a5b81cc12db5e8c84167e9f0d32d121d9843dc97 100644 --- a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/biz/BizIdInjectInterceptor.java +++ b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/biz/BizIdInjectInterceptor.java @@ -7,8 +7,6 @@ import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlCommandType; -import java.sql.SQLException; - /** * @author liaozan * @since 2023-04-17 @@ -16,13 +14,12 @@ import java.sql.SQLException; public class BizIdInjectInterceptor implements InnerInterceptor { @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(); if (sqlCommandType != SqlCommandType.INSERT) { return; } - Class entityClass = entity.getClass(); - BizIdColumnField bizColumnField = BizIdHelper.getBizColumnField(entityClass); + BizIdColumnField bizColumnField = BizIdHelper.getBizColumnField(entity.getClass()); if (bizColumnField == null) { return; } diff --git a/starters/oss-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/oss/OssAutoConfiguration.java b/starters/oss-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/oss/OssAutoConfiguration.java index d5eebe957462387abde67ba851e53d8cc236c221..7a7e6d8949772fba219e7750069a02f07347d1cc 100644 --- a/starters/oss-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/oss/OssAutoConfiguration.java +++ b/starters/oss-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/oss/OssAutoConfiguration.java @@ -4,8 +4,6 @@ import com.schbrain.framework.autoconfigure.oss.properties.OssProperties; import com.schbrain.framework.autoconfigure.oss.util.OssUtils; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.core.env.ConfigurableEnvironment; /** * @author liaozan @@ -15,9 +13,8 @@ import org.springframework.core.env.ConfigurableEnvironment; @EnableConfigurationProperties(OssProperties.class) public class OssAutoConfiguration { - public OssAutoConfiguration(ConfigurableApplicationContext applicationContext, OssProperties ossProperties) { - ConfigurableEnvironment environment = applicationContext.getEnvironment(); - OssUtils.initialize(environment, ossProperties); + public OssAutoConfiguration(OssProperties ossProperties) { + OssUtils.initialize(ossProperties); } } \ No newline at end of file diff --git a/starters/oss-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/oss/util/OssUtils.java b/starters/oss-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/oss/util/OssUtils.java index 6ba2b0b00bf60d9fee78129a93c4f26f42029581..a7472736c56698d937390efc799128bb8bfa976a 100644 --- a/starters/oss-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/oss/util/OssUtils.java +++ b/starters/oss-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/oss/util/OssUtils.java @@ -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.StsProperties; import lombok.extern.slf4j.Slf4j; -import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.util.StringUtils; import java.io.File; @@ -48,7 +47,7 @@ public class OssUtils { private static StsProperties stsProperties; private static String directory; - public static void initialize(ConfigurableEnvironment environment, OssProperties properties) { + public static void initialize(OssProperties properties) { if (properties == null || properties.isInValid()) { log.warn("ossProperties is invalid, OssUtils will not available until reinitialize with the correct configuration"); return; @@ -60,7 +59,7 @@ public class OssUtils { stsProperties = properties.getSts(); directory = properties.getDirectory(); if (directory == null) { - directory = ApplicationName.get(environment); + directory = ApplicationName.get(); } } catch (Exception e) { log.warn("oss initialize fail, OssUtils will not available until reinitialize with the correct configuration", e); diff --git a/starters/pom.xml b/starters/pom.xml index 97e34b2346f94555428211c00f94acdce0950fa7..f04c8db75c479a092c26daa721609734fcec70a8 100644 --- a/starters/pom.xml +++ b/starters/pom.xml @@ -9,7 +9,6 @@ com.schbrain.framework schbrain-parent ${revision} - starters diff --git a/starters/xxl-job-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/xxl/XxlJobAutoConfiguration.java b/starters/xxl-job-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/xxl/XxlJobAutoConfiguration.java index a29b64f9d3694bda78e3ee015da36792f60d5529..874a13b17337a9ee19e2b6c9a22c4851eaaa3555 100644 --- a/starters/xxl-job-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/xxl/XxlJobAutoConfiguration.java +++ b/starters/xxl-job-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/xxl/XxlJobAutoConfiguration.java @@ -10,7 +10,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; -import org.springframework.core.env.ConfigurableEnvironment; import java.nio.file.Paths; @@ -25,15 +24,12 @@ public class XxlJobAutoConfiguration { @Bean @ConditionalOnMissingBean(XxlJobExecutor.class) - public SchbrainXxlJobExecutor schbrainXxlJobSpringExecutor(ConfigurableEnvironment environment, - XxlJobProperties xxlJobProperties, - LoggerProperties loggingProperties) { - String applicationName = ApplicationName.get(environment); + public SchbrainXxlJobExecutor schbrainXxlJobSpringExecutor(XxlJobProperties xxlJobProperties, LoggerProperties loggingProperties) { SchbrainXxlJobExecutor executor = new SchbrainXxlJobExecutor(); executor.setAdminAddresses(xxlJobProperties.getAdminAddresses()); executor.setIp(xxlJobProperties.getIp()); executor.setPort(xxlJobProperties.getPort()); - executor.setAppName(applicationName); + executor.setAppName(ApplicationName.get()); executor.setAccessToken(xxlJobProperties.getAccessToken()); executor.setLogPath(Paths.get(loggingProperties.getLogPath(), "xxl-job").toString()); executor.setLogRetentionDays(xxlJobProperties.getLogRetentionDays()); diff --git a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/EnvironmentPostProcessorLoggerAwareAdapter.java b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/EnvironmentPostProcessorLoggerAwareAdapter.java deleted file mode 100644 index 992860a82c54fefeff2e5e2dbe50f65220d2f92e..0000000000000000000000000000000000000000 --- a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/EnvironmentPostProcessorLoggerAwareAdapter.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.schbrain.framework.support.spring; - -import lombok.Getter; -import org.apache.commons.logging.Log; -import org.springframework.boot.ConfigurableBootstrapContext; -import org.springframework.boot.env.EnvironmentPostProcessor; -import org.springframework.boot.logging.DeferredLogFactory; - -/** - * @author liaozan - * @since 2021/11/22 - */ -@Getter -public abstract class EnvironmentPostProcessorLoggerAwareAdapter implements EnvironmentPostProcessor { - - private final Log log; - private final DeferredLogFactory deferredLogFactory; - private final ConfigurableBootstrapContext bootstrapContext; - - public EnvironmentPostProcessorLoggerAwareAdapter(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) { - this.log = logFactory.getLog(getClass()); - this.bootstrapContext = bootstrapContext; - this.deferredLogFactory = logFactory; - } - -} \ No newline at end of file diff --git a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/LoggerAwareEnvironmentPostProcessor.java b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/LoggerAwareEnvironmentPostProcessor.java new file mode 100644 index 0000000000000000000000000000000000000000..d10721719f9e790627835a1f6aa06dfba14ee800 --- /dev/null +++ b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/LoggerAwareEnvironmentPostProcessor.java @@ -0,0 +1,33 @@ +package com.schbrain.framework.support.spring; + +import lombok.Getter; +import org.apache.commons.logging.Log; +import org.springframework.boot.BootstrapContext; +import org.springframework.boot.ConfigurableBootstrapContext; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.boot.logging.DeferredLogFactory; +import org.springframework.context.ConfigurableApplicationContext; + +/** + * @author liaozan + * @since 2021/11/22 + */ +@Getter +public abstract class LoggerAwareEnvironmentPostProcessor implements EnvironmentPostProcessor { + + protected final Log log; + protected final DeferredLogFactory deferredLogFactory; + protected final ConfigurableBootstrapContext bootstrapContext; + + public LoggerAwareEnvironmentPostProcessor(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) { + this.log = logFactory.getLog(getClass()); + this.bootstrapContext = bootstrapContext; + 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 diff --git a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/defaults/DefaultPropertiesEnvironmentPostProcessor.java b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/defaults/DefaultPropertiesEnvironmentPostProcessor.java index b0286cf36d7c30daa454c929abbcc736c2ed1220..440c8476cd4dca5722d75d98daba1f997b236e87 100644 --- a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/defaults/DefaultPropertiesEnvironmentPostProcessor.java +++ b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/defaults/DefaultPropertiesEnvironmentPostProcessor.java @@ -5,7 +5,7 @@ import cn.hutool.core.util.ArrayUtil; import com.schbrain.common.constants.DateTimeFormatters; import com.schbrain.common.util.EnvUtils; 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.actuate.autoconfigure.health.HealthProperties.Show; import org.springframework.boot.actuate.info.InfoPropertiesInfoContributor.Mode; @@ -23,7 +23,7 @@ import java.util.*; * @author liaozan * @since 2021/12/18 */ -public class DefaultPropertiesEnvironmentPostProcessor extends EnvironmentPostProcessorLoggerAwareAdapter implements Ordered { +public class DefaultPropertiesEnvironmentPostProcessor extends LoggerAwareEnvironmentPostProcessor implements Ordered { /** * set default properties after configData loaded @@ -80,7 +80,7 @@ public class DefaultPropertiesEnvironmentPostProcessor extends EnvironmentPostPr if (ArrayUtil.isEmpty(environment.getActiveProfiles())) { environment.setActiveProfiles(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 return; } if (!environment.containsProperty(DUBBO_REGISTER_KEY)) { - getLog().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("Not running on CloudPlatform, {} is set to false by default", 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); } }