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 中也有这些基础方法


相关文章
|
3月前
|
NoSQL Java Redis
SpringBoot-引入Redis依赖
本文介绍如何在IDEA里将SpringBoot整合Redis。
111 0
|
20天前
|
Java 测试技术
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
21 0
|
1月前
|
Java 数据库连接 Spring
Spring Boot 3.2.2整合MyBatis-Plus 3.5.5依赖不兼容问题
Spring Boot 3.2.2整合MyBatis-Plus 3.5.5依赖不兼容问题
51 0
|
1月前
|
消息中间件 运维 监控
|
3月前
|
前端开发 Java Spring
使用Spring Boot集成Shiro时出现了无法注入Service的问题
使用Spring Boot集成Shiro时出现了无法注入Service的问题
|
4月前
|
NoSQL Java API
SpringBoot【ElasticSearch集成 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(依赖+配置+增删改查测试源码)推荐使用
SpringBoot【ElasticSearch集成 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(依赖+配置+增删改查测试源码)推荐使用
58 0
|
4月前
|
Java API
SpringBoot【集成ElasticSearch 01】2种方式的高级客户端 RestHighLevelClient 使用(依赖+配置+客户端API测试源码)
SpringBoot【集成ElasticSearch 01】2种方式的高级客户端 RestHighLevelClient 使用(依赖+配置+客户端API测试源码)
84 0
|
4月前
|
SQL 前端开发 Java
Hasor【环境搭建 01】SpringBoot集成Dataway接口配置服务(依赖+配置+数据库数据源初始化+注解添加+demo验证测试)
Hasor【环境搭建 01】SpringBoot集成Dataway接口配置服务(依赖+配置+数据库数据源初始化+注解添加+demo验证测试)
73 0
|
19天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
27天前
|
Java API Spring
SpringBoot项目调用HTTP接口5种方式你了解多少?
SpringBoot项目调用HTTP接口5种方式你了解多少?
80 2