当前位置: 首页 > news >正文

概述政府网站建设工作的整体概况湖南百度seo

概述政府网站建设工作的整体概况,湖南百度seo,如何在b2b网站做外链,做网站需要什么证明嘛Spring Boot解决循环依赖注入问题 代码问题回显启动错误日志解决方案:使用事件驱动或通过 ApplicationContext 手动获取 Bean1. 事件驱动设计2. 使用 ApplicationContext 手动获取 Bean3. 拆分逻辑 总结 代码问题回显 现有代码1 在InterestService中依赖MemberInte…

Spring Boot解决循环依赖注入问题

  • 代码问题回显
  • 启动错误日志
  • 解决方案:使用事件驱动或通过 ApplicationContext 手动获取 Bean
    • 1. 事件驱动设计
    • 2. 使用 `ApplicationContext` 手动获取 Bean
    • 3. 拆分逻辑
  • 总结

代码问题回显

现有代码1 在InterestService中依赖MemberInterestService

@Service
@AllArgsConstructor
public class InterestService {// 注意此处循环依赖注入private final MemberInterestService memberInterestService;private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);/*** 调度下一个利息任务*/public void scheduleNextInterestTask() {// 省略其他代码...}
}

现有代码2 在MemberInterestService实现类中注入InterestService

@Service
@AllArgsConstructor
public class MemberInterestServiceImpl extends ServiceImpl<MemberInterestMapper, MemberInterest> implements MemberInterestService {// 注意此处循环依赖注入private final InterestService interestService;@Overridepublic Boolean updateExpireStatus(MemberInterestExpireStatus body) {// 省略其他代码...if (updateById(interest)){// TODO 此处出现循环依赖注入(直接报错)interestService.scheduleNextInterestTask();return true;}}
}

启动错误日志

Description:
The dependencies of some of the beans in the application context form a cycle:   
mobileLoginController (field private com.sinbyte.framework.web.service.SysLoginService 
com.sinbyte.web.controller.system.MobileLoginController.loginService)      
↓   
sysLoginService (field private com.sinbyte.ray.service.MemberUserService 
com.sinbyte.framework.web.service.SysLoginService.memberUserService)
┌─────┐
|  memberInterestServiceImpl defined in file [D:\Java\IdeaProjects\alliance-server\alliance-ray\target\classes\com\sinbyte\ray\service\impl\MemberInterestServiceImpl.class]
↑     ↓
|  interestService defined in file [D:\Java\IdeaProjects\alliance-server\alliance-ray\target\classes\com\sinbyte\ray\delay\InterestService.class]
└─────┘

在场景中, MemberInterestServiceImpl 需要调用 InterestServicescheduleNextInterestTask() 方法,但由于这两个服务之间存在循环依赖,直接注入会导致 Spring 启动时发生循环依赖错误

解决方案:使用事件驱动或通过 ApplicationContext 手动获取 Bean

以下是一些可以解决循环依赖问题的方法:

1. 事件驱动设计

可以使用 Spring 的事件机制,将调用 scheduleNextInterestTask 的操作转变为事件驱动。具体做法是,当 MemberInterestServiceImpl 需要调用 scheduleNextInterestTask 时,发布一个自定义事件,InterestService 监听这个事件并执行相应的任务。

首先,定义一个自定义事件类:

import org.springframework.context.ApplicationEvent;
/*** 自定义事件驱动* @CreateDate: 2024/8/14 15:30*/
public class InterestTaskEvent extends ApplicationEvent {public InterestTaskEvent(Object source) {super(source);}
}

接着,在 MemberInterestServiceImpl 中发布事件,实现ApplicationEventPublisherAware并重写setApplicationEventPublisher方法:

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;@Service
@AllArgsConstructor
public class MemberInterestServiceImpl extends ServiceImpl<MemberInterestMapper, MemberInterest> implements MemberInterestService, ApplicationEventPublisherAware {// 事件发布器private ApplicationEventPublisher eventPublisher;@Overridepublic void setApplicationEventPublisher(@NotNull ApplicationEventPublisher applicationEventPublisher) {// 注入事件发布器this.eventPublisher = applicationEventPublisher;}@Overridepublic Boolean updateExpireStatus(MemberInterestExpireStatus body) {// 省略其他代码...if (updateById(interest)) {// 省略其他代码...eventPublisher.publishEvent(new InterestTaskEvent(this));  // 发布事件return true;}}
}

InterestService 中监听这个事件:

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;@Service
@AllArgsConstructor
public class InterestService {// 其他依赖...@EventListenerpublic void onInterestTaskEvent(InterestTaskEvent event) {scheduleNextInterestTask();}public void scheduleNextInterestTask() {// 省略其他代码...}
}

2. 使用 ApplicationContext 手动获取 Bean

如果你不希望使用事件驱动,还可以通过 Spring 的 ApplicationContext 手动获取 InterestService Bean,从而避免循环依赖。

MemberInterestServiceImpl 中引入 ApplicationContext 并在需要调用时获取 InterestService

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;@Service
@Slf4j
@AllArgsConstructor
public class MemberInterestServiceImpl extends ServiceImpl<MemberInterestMapper, MemberInterest> implements MemberInterestService, ApplicationContextAware {private ApplicationContext applicationContext;private final RedisCache redisCache;private final PublicService publicService;private final MemberApplyBusinessService memberApplyBusinessService;private final MemberTransactionRecordService memberTransactionRecordService;private final MemberInterestPointService memberInterestPointService;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) {this.applicationContext = applicationContext;}@Overridepublic Boolean updateExpireStatus(MemberInterestExpireStatus body) {// 省略其他代码...if (updateById(interest)) {// 省略其他代码...InterestService interestService = applicationContext.getBean(InterestService.class);interestService.scheduleNextInterestTask();  // 手动获取 Bean 并调用方法return true;}throw new ServiceException("更新失败");}
}

3. 拆分逻辑

如果可能,考虑将 InterestService 的部分逻辑拆分到一个新的服务中,以减少 InterestServiceMemberInterestServiceImpl 之间的依赖关系。这可能需要对业务逻辑进行一定的重构,但从长期维护的角度来看,是一种更优雅的解决方案。

总结

通过以上方法,可以有效地解决循环依赖问题并在 MemberInterestServiceImpl 中安全地调用 InterestService 的方法。推荐使用事件驱动的方法,这不仅解决了循环依赖问题,还能让你的代码更具扩展性和松耦合。

生活不能过度的平坦,这样的生活才最有意义!

http://www.mnyf.cn/news/41802.html

相关文章:

  • 锚文本外链网站seo网络推广优势
  • 淘宝做网站骗局日本shopify独立站
  • 如何做旅游小视频网站站长工具爱情岛
  • 新品发布会的策划方案如何进行搜索引擎优化
  • 企业模板网站建设优势分析泰州seo推广
  • 温州网站推广效果好长沙seo关键词
  • 多用户网站建设方案新手销售怎么和客户交流
  • 网站对联广告html代码推推蛙seo顾问
  • 大型网站建设哪家服务好杭州seo网站建设靠谱
  • 做网站的图片用什么格式自助建站系统平台
  • 网站日常优化搭建一个网站需要什么
  • 网站页面多少抖音seo软件工具
  • 苏州网站建设设计免费浏览外国网站的软件
  • 辽宁省建设银行网站如何添加百度指数
  • 前端怎么在猪八戒网站接单做软文广告经典案例600
  • 中文一级a做爰片免费网站谷歌搜索优化
  • 公众号涨粉桔子seo工具
  • 成都学校网站制作网络整合营销案例
  • 儋州网站建设百度收录查询接口
  • 教育培训网站案例如何自己创建网址
  • 兰州微信信息平台网站建设广东网站关键词排名
  • 创意旅行社wordpress西安百度提升优化
  • wordpress改密码忘记seo公司优化排名
  • wp建站模板企业网站大全
  • 建e室内设计网全景效果图短视频seo搜索优化
  • 呼市网站制作营销培训课程2022
  • 导购网站自己做电商论坛平台
  • 网站服务器租赁费用表格微信广告投放收费标准
  • 做实验学编程的网站新人做外贸怎么找国外客户
  • 微信订阅号不认证可以做网站吗企业宣传片视频