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


相关文章
|
Java Maven 微服务
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的 maven 依赖
在项目中使用Swagger2工具时,需导入Maven依赖。尽管官方最高版本为2.8.0,但其展示效果不够理想且稳定性欠佳。实际开发中常用2.2.2版本,因其稳定且界面友好。以下是围绕2.2.2版本的Maven依赖配置,包括`springfox-swagger2`和`springfox-swagger-ui`两个模块。
792 0
|
缓存 Java 应用服务中间件
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——依赖导入和Thymeleaf相关配置
在Spring Boot中使用Thymeleaf模板,需引入依赖`spring-boot-starter-thymeleaf`,并在HTML页面标签中声明`xmlns:th="http://www.thymeleaf.org"`。此外,Thymeleaf默认开启页面缓存,开发时建议关闭缓存以实时查看更新效果,配置方式为`spring.thymeleaf.cache: false`。这可避免因缓存导致页面未及时刷新的问题。
671 0
|
Java Spring 容器
Springboot3.2.1搞定了类Service和bean注解同名同类型问题修复
这篇文章讨论了在Spring Boot 3.2.1版本中,同名同类型的bean和@Service注解类之间冲突的问题得到了解决,之前版本中同名bean会相互覆盖,但不会在启动时报错,而在配置文件中设置`spring.main.allow-bean-definition-overriding=true`可以解决这个问题。
1223 0
Springboot3.2.1搞定了类Service和bean注解同名同类型问题修复
|
Java Windows
SpringBoot Windows 自启动 - 通过 Windows Service 服务实现
SpringBoot Windows 自启动 - 通过 Windows Service 服务实现
1156 2
SpringBoot第一次导入项目,Maven依赖全爆红,该怎样解决,idea2019.3版本,必须用application2.7.6或者以下
SpringBoot第一次导入项目,Maven依赖全爆红,该怎样解决,idea2019.3版本,必须用application2.7.6或者以下
|
Java 数据库连接 mybatis
SpringBoot配置Mybatis注意事项,mappers层下的name命名空间,要落实到Dao的video类,resultType要落到bean,配置好mybatis的对应依赖。
SpringBoot配置Mybatis注意事项,mappers层下的name命名空间,要落实到Dao的video类,resultType要落到bean,配置好mybatis的对应依赖。
|
前端开发 JavaScript Java
【Azure 应用服务】App Service For Windows 中如何设置代理实现前端静态文件和后端Java Spring Boot Jar包
【Azure 应用服务】App Service For Windows 中如何设置代理实现前端静态文件和后端Java Spring Boot Jar包
344 0
|
Java Linux C++
【Azure 应用服务】App Service For Linux 部署Java Spring Boot应用后,查看日志文件时的疑惑
【Azure 应用服务】App Service For Linux 部署Java Spring Boot应用后,查看日志文件时的疑惑
331 0
|
Java 文件存储
软件开发常用之SpringBoot文件上传和下载功能(上){fileName},利用hutool提供的依赖,拿到当前目录的路径,System.getProperty从变量获取路径 ,不存在就用mkdi
软件开发常用之SpringBoot文件上传和下载功能(上){fileName},利用hutool提供的依赖,拿到当前目录的路径,System.getProperty从变量获取路径 ,不存在就用mkdi
|
Java Maven Spring
Spring Boot中的依赖管理策略
Spring Boot中的依赖管理策略