refactor: 删除heritage-common配置类

- 移除异步线程池配置AsyncConfig
- 移除Jackson序列化配置JacksonConfig
- 移除Long类型序列化配置LongSerializerConfig
- 移除Redis序列化配置RedisConfig
- 清理过时的基础设施配置
This commit is contained in:
Leo 2025-10-17 11:07:39 +08:00
parent cc94ec159a
commit 98f3964a4f
4 changed files with 0 additions and 312 deletions

View File

@ -1,71 +0,0 @@
package org.leocoder.heritage.common.config;
import cn.hutool.core.lang.UUID;
import org.slf4j.MDC;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @description [多线程核心配置类]
* @author : Leocoder
*/
@Configuration
@EnableAsync
public class AsyncConfig {
/**
* cpu个数查询方式Runtime.getRuntime().availableProcessors()
* 其中的corePoolSize设置参考N为cpu个数
* 如果是CPU密集型应用则线程池大小设置为 N+1
* 如果是IO密集型应用则线程池大小设置为 2N+1
* Linux命令查询方式
* cat /proc/cpuinfo| grep "processor"| wc -l
*/
private final int core = Runtime.getRuntime().availableProcessors() + 1;
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程数线程池创建时候初始化的线程数
executor.setCorePoolSize(core + 1);
// 最大线程数线程池最大的线程数只有在缓冲队列满了之后才会申请超过核心线程数的线程
executor.setMaxPoolSize(core * 2);
// 缓冲队列用来缓冲执行任务的队列
executor.setQueueCapacity(128);
// 允许线程的空闲时间60秒当超过了核心线程之外的线程在空闲时间到达之后会被销毁
executor.setKeepAliveSeconds(60);
// 线程池名的前缀设置好了之后可以方便我们定位处理任务所在的线程池
executor.setThreadNamePrefix("CoderAsyncExecutor-");
// 缓冲队列满了之后的拒绝策略不在新线程中执行任务而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 设置线程装饰器
executor.setTaskDecorator(runnable -> {
Map<String, String> contextMap = MDC.getCopyOfContextMap();
return () -> {
if (contextMap == null) {
MDC.clear();
} else {
MDC.setContextMap(contextMap);
}
if (MDC.get("traceId") == null) {
MDC.put("traceId", UUID.fastUUID().toString(true));
}
try {
runnable.run();
} finally {
MDC.clear();
}
};
});
// 初始化线程池, 初始化 core 线程
executor.initialize();
return executor;
}
}

View File

@ -1,105 +0,0 @@
package org.leocoder.heritage.common.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.TimeZone;
/**
* @author : Leocoder
* @description [JacksonConfig]
*/
@Slf4j
@Configuration
public class JacksonConfig {
@Primary
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
configureObjectMapper(objectMapper);
log.info("启动Jackson配置");
return objectMapper;
}
/**
* @description [数据返回配置]
* @author : Leocoder
*/
private void configureObjectMapper(ObjectMapper objectMapper) {
// JsonInclude.Include.ALWAYS 所以属性都必须在
// JsonInclude.Include.NON_NULL 如果你属性是null就会自动剔除
// JsonInclude.Include.NON_EMPTY 如果你属性是""就会自动剔除
// objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
// 对象的所有字段全部列入
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
// 忽略空Bean转json的错误
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// 忽略在json字符串中存在但是在java对象中不存在对应属性的情况防止错误
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 精度的转换问题
objectMapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.setLocale(Locale.CHINA);
// 设置时区
objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
// 配置日期和时间格式
configureDateAndTimeFormats(objectMapper);
// 配置Long和BigInteger的自定义序列化器
configureCustomSerializers(objectMapper);
}
/**
* @description [配置日期和时间格式]
* @author : Leocoder
*/
private void configureDateAndTimeFormats(ObjectMapper objectMapper) {
// JavaTimeModule 用于指定序列化和反序列化规则
JavaTimeModule javaTimeModule = new JavaTimeModule();
// 支持 LocalDateTimeLocalDateLocalTime
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
objectMapper.registerModule(javaTimeModule);
}
/**
* @description [配置Long和BigInteger的自定义序列化器]
* @author : Leocoder
*/
private void configureCustomSerializers(ObjectMapper objectMapper) {
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, LongSerializerConfig.INSTANCE);
simpleModule.addSerializer(Long.TYPE, LongSerializerConfig.INSTANCE);
simpleModule.addSerializer(BigInteger.class, LongSerializerConfig.INSTANCE);
objectMapper.registerModule(simpleModule);
}
}

View File

@ -1,42 +0,0 @@
package org.leocoder.heritage.common.config;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
import java.io.IOException;
/**
* @author : Leocoder
* @description [LongSerializerConfig-Long类型序列化]
*/
@JacksonStdImpl
public class LongSerializerConfig extends NumberSerializer {
/**
* 根据 JS Number.MAX_SAFE_INTEGER Number.MIN_SAFE_INTEGER 得来
*/
private static final long MAX_SAFE_INTEGER = 9007199254740991L;
private static final long MIN_SAFE_INTEGER = -9007199254740991L;
/**
* 提供实例
*/
public static final LongSerializerConfig INSTANCE = new LongSerializerConfig(Number.class);
public LongSerializerConfig(Class<? extends Number> rawType) {
super(rawType);
}
@Override
public void serialize(Number value, JsonGenerator gen, SerializerProvider provider) throws IOException {
// 超出范围 序列化位字符串
if (value.longValue() > MIN_SAFE_INTEGER && value.longValue() < MAX_SAFE_INTEGER) {
super.serialize(value, gen, provider);
} else {
gen.writeString(value.toString());
}
}
}

View File

@ -1,94 +0,0 @@
package org.leocoder.heritage.common.config;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* @author : Leocoder
* @description [Redis配置类]
*/
@Configuration
public class RedisConfig implements CachingConfigurer {
// 标准日期
private static final String STANDARD_PATTERN = "yyyy-MM-dd HH:mm:ss";
// 年月日格式
private static final String DATE_PATTERN = "yyyy-MM-dd";
// 时间格式
private static final String TIME_PATTERN = "HH:mm:ss";
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 使用Jackson2JsonRedisSerialize 替换默认序列化
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(),
ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.PROPERTY);
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
// LocalDateTime日期序列化处理
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
this.registerLocalDateTime(objectMapper);
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(objectMapper, Object.class);
template.setConnectionFactory(connectionFactory);
// key采用String的序列化方式
template.setKeySerializer(new StringRedisSerializer());
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(new StringRedisSerializer());
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
/**
* @description [处理时间类型]
* @author : Leocoder
*/
private void registerLocalDateTime(ObjectMapper objectMapper) {
// 设置java.util.Date时间类的序列化以及反序列化的格式
objectMapper.setDateFormat(new SimpleDateFormat(STANDARD_PATTERN));
JavaTimeModule timeModule = new JavaTimeModule();
// LocalDateTime
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(STANDARD_PATTERN);
timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
// LocalDate
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter));
timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter));
// LocalTime
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(TIME_PATTERN);
timeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(timeFormatter));
timeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(timeFormatter));
objectMapper.registerModule(timeModule);
}
}