Springboot的service循环依赖

简介: Springboot的service循环依赖

项目中Service经常使用lombok提供的注解@RequiredArgsConstructor或者@AllArgsConstructor来注入其他Service


注解 @RequiredArgsConstructor生成带有必需参数的构造函数。 必需的参数是最终字段和具有约束的字段,例如@NonNull 。@AllArgsConstructor则是全部

但是使用的时候可能会存在循环依赖的问题


┌─────┐
|  sysUserV2ServiceImpl defined in URL [jar:file:/home/starwiz/data/hyt/starwiz.jar!/BOOT-INF/classes!/com/starwiz/project/system/v2/service/impl/SysUserV2ServiceImpl.class]
↑     ↓
|  processServiceImpl (field com.starwiz.project.workflow.WorkflowUtil com.starwiz.project.workflow.service.impl.ProcessServiceImpl.workflowUtil)
↑     ↓
|  workflowUtil (field com.starwiz.project.system.service.SysTodoService com.starwiz.project.workflow.WorkflowUtil.sysTodoService)
↑     ↓
|  sysTodoService (field private com.starwiz.project.warning.service.EarlyWarningFarmerService com.starwiz.project.system.service.impl.SysTodoServiceImpl.earlyWarningFarmerService)
↑     ↓
|  earlyWarningFarmerService (field com.starwiz.project.workflow.service.WorkflowService com.starwiz.project.warning.service.impl.EarlyWarningFarmerServiceImpl.workflowService)
↑     ↓
|  workflowServiceImpl (field com.starwiz.project.system.v2.service.SysUserV2Service com.starwiz.project.workflow.service.impl.WorkflowServiceImpl.sysUserV2Service)
└─────┘


  • 方法1


@Autowired
@Lazy //懒加载


  • 方法2


// lombok的注解 
@AllArgsConstructor


  • 方法3 使用 setter/field 方法注入
private DepartmentService departmentSerivce;
@Autowired
public void setDepartmentSerivce(DepartmentService departmentService) {
    this.departmentService = departmentService;
}
  • 方法4 使用 @PostConstruct
@Service
public class UserService {
    @Autowired
    private DepartmentService departmentService;
    @PostConstruct
    public void init() {
        departmentService.setUserService(this);
    }
    public DepartmentService getDepartmentService() {
        return departmentService;
    }
}


  • 方法5 实现ApplicationContextAware and InitializingBean接口
@Service
public class UserService implements ApplicationContextAware, InitializingBean {
    private DepartmentService departmentService;
    private ApplicationContext context;
    public DepartmentService getDepartmentService() {
        return departmentService;
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        circB = context.getBean(DepartmentService.class);
    }
    @Override
    public void setApplicationContext(final ApplicationContext ctx) throws BeansException {
        context = ctx;
    }
}
  • 使用Mybatis Plus時,可以将依赖 Service 换成依赖 Mapper,因为 Mapper 中也有这些基础方







相关文章
|
20小时前
|
Java Spring
springboot 集成 swagger 2.x 和 3.0 以及 Failed to start bean ‘documentationPluginsBootstrapper‘问题的解决
本文介绍了如何在Spring Boot项目中集成Swagger 2.x和3.0版本,并提供了解决Swagger在Spring Boot中启动失败问题“Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerEx”的方法,包括配置yml文件和Spring Boot版本的降级。
springboot 集成 swagger 2.x 和 3.0 以及 Failed to start bean ‘documentationPluginsBootstrapper‘问题的解决
|
1月前
|
Java 数据安全/隐私保护
SpringBoot 自定义初始化任务 Runner
SpringBoot 自定义初始化任务 Runner
10 0
|
10月前
|
安全 Java 网络安全
SpringBoot 优雅停止服务的几种方法
SpringBoot 优雅停止服务的几种方法
197 0
|
安全 Java 网络安全
Springboot 优雅停止服务的几种方法
Springboot 优雅停止服务的几种方法
|
4月前
|
Java
SpringBoot集成swagger后出现: Failed to start bean ‘documentationPluginsBootstrapper‘的解决方法
SpringBoot集成swagger后出现: Failed to start bean ‘documentationPluginsBootstrapper‘的解决方法
130 0
|
10月前
|
Java 数据库连接 mybatis
Springboot的service循环依赖
Springboot的service循环依赖
|
存储 网络协议 Java
SpringBoot在自定义实现类中调用service层等Spring其他层报错为null的解决办法
SpringBoot在自定义实现类中调用service层等Spring其他层报错为null的解决办法
524 0
Java:Springboot的service循环依赖问题
Java:Springboot的service循环依赖问题
158 0
|
Java
Springboot启动后执行方法
Springboot启动后执行方法
221 0
自定义springboot-start
自定义springboot-start
83 0