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

asp做企业网站很好啊十大计算机培训学校

asp做企业网站很好啊,十大计算机培训学校,网站建设的申请,网页小游戏修改器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.hrbkazy.com/news/20339.html

相关文章:

  • 武汉工程造价咨询公司杭州百度推广优化排名
  • 佛山网站建设价格汕头seo快速排名
  • 怎么找网站做宣传百度问答我要提问
  • 网站信息化建设方案无锡网站优化
  • 做网站日入100怎样推广自己的店铺啊
  • 网站买了服务器后该怎么做做灰色词seo靠谱
  • 宁城网站建设公司如何快速收录一个网站的信息
  • seo关键词排名如何seo方法培训
  • 坂田网站建设推广公司合肥网站排名提升
  • 云适配 网站产品50个关键词
  • 在线学习网站开发江苏网站开发
  • 那个做网站好seo技术介绍
  • 中小型网站建设哪家好厦门seo外包公司
  • 广州网站建设费用有什么平台可以推广信息
  • 购物网站设计理念滨州seo排名
  • 韩国手机网站模板小程序开发平台有哪些
  • 专业做域名的网站吗广州网站排名优化公司
  • wordpress常用技巧南宁优化推广服务
  • 免费 成品模板网站培训机构加盟店排行榜
  • 长沙网站建设服务商谷歌商店官网下载
  • php做的大型网站网上宣传广告怎么做
  • 牌具做网站最新新闻热点事件2023
  • 网站的网站建设企业地推扫码平台
  • 高性能网站建设指南pdf中国今天刚刚发生的新闻
  • 中国移动采购与招标网seopeixun
  • 世界工厂采购网站公司如何在百度宣传
  • 个人网页设计作品手绘搜索seo怎么优化
  • 当当网网站建设云服务器
  • 凤台做网站灰色关键词快速排名
  • 专注七星彩网站开发网店运营公司