refactor(core): 添加 Lazy 注解解决循环依赖问题

- 在多个 Service 类中为注入的依赖添加 @Lazy 注解
- 修改了微信支付服务实现类中的依赖注入方式
- 更新了打印机服务实现类中的依赖注入配置
- 调整了统计拦截器和服务类中的依赖注入策略
- 优化了 FaceService 和相关 Repository 的注入方式
- 防止应用启动时因循环依赖导致的初始化失败
This commit is contained in:
2025-12-05 15:21:01 +08:00
parent 82626f615b
commit 71a8d3b539
5 changed files with 21 additions and 15 deletions

View File

@@ -5,38 +5,23 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator; import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
/** /**
* @author wenshijia * @author wenshijia
* @date 2021年07月05日 18:34 * @date 2021年07月05日 18:34
* 修改redis缓存序列化器 * 修改redis缓存序列化器
*/ */
@Configuration @Configuration
@EnableCaching
public class CustomRedisCacheManager { public class CustomRedisCacheManager {
@Autowired @Autowired
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(objectMapper, Object.class);
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).entryTtl(Duration.ofMinutes(1));
return configuration;
}
/** /**
* 处理redis连接工具显示redis key值显示乱码问题,value值没处理 * 处理redis连接工具显示redis key值显示乱码问题,value值没处理
* *

View File

@@ -30,6 +30,7 @@ import com.ycwl.basic.service.pc.ScenicService;
import com.ycwl.basic.utils.SnowFlakeUtil; import com.ycwl.basic.utils.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -50,18 +51,25 @@ import java.util.*;
public class WxPayServiceImpl implements WxPayService { public class WxPayServiceImpl implements WxPayService {
@Autowired @Autowired
@Lazy
private PaymentMapper paymentMapper; private PaymentMapper paymentMapper;
@Autowired @Autowired
@Lazy
private StatisticsMapper statisticsMapper; private StatisticsMapper statisticsMapper;
@Autowired @Autowired
@Lazy
private OrderRepository orderRepository; private OrderRepository orderRepository;
@Autowired @Autowired
@Lazy
private OrderBiz orderBiz; private OrderBiz orderBiz;
@Autowired @Autowired
@Lazy
private OrderMapper orderMapper; private OrderMapper orderMapper;
@Autowired @Autowired
@Lazy
private ScenicService scenicService; private ScenicService scenicService;
@Autowired @Autowired
@Lazy
private RedisTemplate<String, String> redisTemplate; private RedisTemplate<String, String> redisTemplate;
@Override @Override

View File

@@ -119,29 +119,37 @@ public class PrinterServiceImpl implements PrinterService {
@Lazy @Lazy
private OrderBiz orderBiz; private OrderBiz orderBiz;
@Autowired @Autowired
@Lazy
private WxPayService wxPayService; private WxPayService wxPayService;
@Autowired @Autowired
private PrintTaskMapper printTaskMapper; private PrintTaskMapper printTaskMapper;
@Autowired @Autowired
@Lazy
private IPriceCalculationService priceCalculationService; private IPriceCalculationService priceCalculationService;
@Autowired @Autowired
@Lazy
private IAutoCouponService autoCouponService; private IAutoCouponService autoCouponService;
@Autowired @Autowired
@Lazy
private ScenicRepository scenicRepository; private ScenicRepository scenicRepository;
@Autowired @Autowired
@Lazy
private OrderRepository orderRepository; private OrderRepository orderRepository;
@Autowired @Autowired
private FaceSampleMapper faceSampleMapper; private FaceSampleMapper faceSampleMapper;
@Autowired @Autowired
private FaceMapper faceMapper; private FaceMapper faceMapper;
@Autowired @Autowired
@Lazy
private FaceRepository faceRepository; private FaceRepository faceRepository;
@Lazy @Lazy
@Autowired @Autowired
private FaceService faceService; private FaceService faceService;
@Autowired @Autowired
@Lazy
private DeviceRepository deviceRepository; private DeviceRepository deviceRepository;
@Autowired @Autowired
@Lazy
private PrinterTaskPushService taskPushService; private PrinterTaskPushService taskPushService;
// 用于优先打印的线程池,核心线程数根据实际情况调整 // 用于优先打印的线程池,核心线程数根据实际情况调整

View File

@@ -6,6 +6,7 @@ import com.ycwl.basic.stats.service.StatsService;
import com.ycwl.basic.stats.util.StatsUtil; import com.ycwl.basic.stats.util.StatsUtil;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
@@ -18,6 +19,7 @@ import java.util.HashSet;
@Component @Component
public class StatsInterceptor implements HandlerInterceptor { public class StatsInterceptor implements HandlerInterceptor {
@Lazy
@Autowired @Autowired
private StatsService statsService; private StatsService statsService;

View File

@@ -7,15 +7,18 @@ import com.ycwl.basic.stats.mapper.StatsMapper;
import com.ycwl.basic.stats.mapper.StatsRecordMapper; import com.ycwl.basic.stats.mapper.StatsRecordMapper;
import com.ycwl.basic.stats.service.StatsService; import com.ycwl.basic.stats.service.StatsService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date; import java.util.Date;
@Service @Service
public class StatsServiceImpl implements StatsService { public class StatsServiceImpl implements StatsService {
@Lazy
@Autowired @Autowired
private StatsMapper statsMapper; private StatsMapper statsMapper;
@Lazy
@Autowired @Autowired
private StatsRecordMapper statsRecordMapper; private StatsRecordMapper statsRecordMapper;