1. 简化 JsonUtils 初始化方式,去掉 BeanPostProcessor

2. 减少日志打印

目的:优化启动速度
This commit is contained in:
YunaiV 2023-09-16 18:05:50 +08:00
parent 50371b3676
commit ab800aa891
5 changed files with 26 additions and 34 deletions

View File

@ -10,7 +10,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.io.IOException; import java.io.IOException;
@ -23,7 +22,6 @@ import java.util.List;
* *
* @author 芋道源码 * @author 芋道源码
*/ */
@UtilityClass
@Slf4j @Slf4j
public class JsonUtils { public class JsonUtils {

View File

@ -40,7 +40,7 @@ public abstract class AbstractSmsClient implements SmsClient {
*/ */
public final void init() { public final void init() {
doInit(); doInit();
log.info("[init][配置({}) 初始化完成]", properties); log.debug("[init][配置({}) 初始化完成]", properties);
} }
/** /**

View File

@ -30,7 +30,7 @@ public abstract class AbstractFileClient<Config extends FileClientConfig> implem
*/ */
public final void init() { public final void init() {
doInit(); doInit();
log.info("[init][配置({}) 初始化完成]", config); log.debug("[init][配置({}) 初始化完成]", config);
} }
/** /**

View File

@ -1,51 +1,42 @@
package cn.iocoder.yudao.framework.jackson.config; package cn.iocoder.yudao.framework.jackson.config;
import cn.iocoder.yudao.framework.jackson.core.databind.NumberSerializer; import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeDeserializer; import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeDeserializer;
import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeSerializer; import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeSerializer;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import cn.iocoder.yudao.framework.jackson.core.databind.NumberSerializer;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List;
@AutoConfiguration @AutoConfiguration
@Slf4j @Slf4j
public class YudaoJacksonAutoConfiguration { public class YudaoJacksonAutoConfiguration {
@Bean @Bean
public BeanPostProcessor objectMapperBeanPostProcessor() { @SuppressWarnings("InstantiationOfUtilityClass")
return new BeanPostProcessor() { public JsonUtils jsonUtils(List<ObjectMapper> objectMappers) {
// 1.1 创建 SimpleModule 对象
SimpleModule simpleModule = new SimpleModule();
simpleModule
// 新增 Long 类型序列化规则数值超过 2^53-1 JS 会出现精度丢失问题因此 Long 自动序列化为字符串类型
.addSerializer(Long.class, NumberSerializer.INSTANCE)
.addSerializer(Long.TYPE, NumberSerializer.INSTANCE)
// 新增 LocalDateTime 序列化反序列化规则
.addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE)
.addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE);
// 1.2 注册到 objectMapper
objectMappers.forEach(objectMapper -> objectMapper.registerModule(simpleModule));
@Override // 2. 设置 objectMapper JsonUtils {
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { JsonUtils.init(CollUtil.getFirst(objectMappers));
if (!(bean instanceof ObjectMapper)) { log.info("[init][初始化 JsonUtils 成功]");
return bean; return new JsonUtils();
}
ObjectMapper objectMapper = (ObjectMapper) bean;
SimpleModule simpleModule = new SimpleModule();
/*
* 1. 新增Long类型序列化规则数值超过2^53-1在JS会出现精度丢失问题因此Long自动序列化为字符串类型
* 2. 新增LocalDateTime序列化反序列化规则
*/
simpleModule
.addSerializer(Long.class, NumberSerializer.INSTANCE)
.addSerializer(Long.TYPE, NumberSerializer.INSTANCE)
.addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE)
.addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE);
objectMapper.registerModules(simpleModule);
JsonUtils.init(objectMapper);
log.info("初始化 jackson 自动配置");
return bean;
}
};
} }
} }

View File

@ -156,9 +156,12 @@ logging:
cn.iocoder.yudao.module.bpm.dal.mysql: debug cn.iocoder.yudao.module.bpm.dal.mysql: debug
cn.iocoder.yudao.module.infra.dal.mysql: debug cn.iocoder.yudao.module.infra.dal.mysql: debug
cn.iocoder.yudao.module.infra.dal.mysql.job.JobLogMapper: INFO # 配置 JobLogMapper 的日志级别为 info cn.iocoder.yudao.module.infra.dal.mysql.job.JobLogMapper: INFO # 配置 JobLogMapper 的日志级别为 info
cn.iocoder.yudao.module.infra.dal.mysql.file.FileConfigMapper: INFO # 配置 FileConfigMapper 的日志级别为 info
cn.iocoder.yudao.module.pay.dal.mysql: debug cn.iocoder.yudao.module.pay.dal.mysql: debug
cn.iocoder.yudao.module.pay.dal.mysql.notify.PayNotifyTaskMapper: INFO # 配置 JobLogMapper 的日志级别为 info cn.iocoder.yudao.module.pay.dal.mysql.notify.PayNotifyTaskMapper: INFO # 配置 JobLogMapper 的日志级别为 info
cn.iocoder.yudao.module.system.dal.mysql: debug cn.iocoder.yudao.module.system.dal.mysql: debug
cn.iocoder.yudao.module.system.dal.mysql.sensitiveword.SensitiveWordMapper: INFO # 配置 SensitiveWordMapper 的日志级别为 info
cn.iocoder.yudao.module.system.dal.mysql.sms.SmsChannelMapper: INFO # 配置 SmsChannelMapper 的日志级别为 info
cn.iocoder.yudao.module.tool.dal.mysql: debug cn.iocoder.yudao.module.tool.dal.mysql: debug
cn.iocoder.yudao.module.member.dal.mysql: debug cn.iocoder.yudao.module.member.dal.mysql: debug
cn.iocoder.yudao.module.trade.dal.mysql: debug cn.iocoder.yudao.module.trade.dal.mysql: debug