SpringBoot 2.x 实战专题——SpringBoot 2.6.X版本循环依赖(内含教学视频+源代码)

news2024/9/24 21:26:20

SpringBoot 2.x 实战专题——SpringBoot 2.6.X版本循环依赖(内含教学视频+源代码)

教学视频+源代码下载链接地址:https://download.csdn.net/download/weixin_46411355/87462754

目录

  • SpringBoot 2.x 实战专题——SpringBoot 2.6.X版本循环依赖(内含教学视频+源代码)
  • `教学视频+源代码下载链接地址:`[https://download.csdn.net/download/weixin_46411355/87462754](https://download.csdn.net/download/weixin_46411355/87462754)
    • 一、创建一个SpringBoot项目
    • 二、修改SpringBoot项目的版本为2.6.11
    • 三、创建2个service类
      • 3.1 RoleService
      • 3.2 UserService
    • 四、运行启动类报错
    • 五、解决循环依赖报错
      • 5.1 方法一,修改application.yml配置文件
      • 5.2 方法二:将成员变量转化为方法调用(引入依赖的方式)
        • 5.2.1 pom.xml 中 引入依赖
        • 5.2.2 将成员变量转化为方法调用
          • 5.2.2.1 RoleService
          • 5.2.2.2 UserService
        • 5.2.3 运行启动类
      • 5.3 方法三:将成员变量转化为方法调用(SpringUtils工具类的方式)
        • 5.3.1 SpringUtils工具类
        • 5.3.2 将成员变量转化为方法调用
          • 5.3.2.1 RoleService
          • 5.3.2.2 UserService
      • 5.4 方法四 抽取成公共的服务类

一、创建一个SpringBoot项目

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

二、修改SpringBoot项目的版本为2.6.11

在这里插入图片描述

三、创建2个service类

在这里插入图片描述

3.1 RoleService

package com.bjowernode.springbootcircledependency.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleService {
    @Autowired
    private UserService userService;
}

3.2 UserService

package com.bjowernode.springbootcircledependency.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private RoleService roleService;
}

四、运行启动类报错

在这里插入图片描述

报错信息

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  roleService (field private com.bjowernode.springbootcircledependency.service.UserService com.bjowernode.springbootcircledependency.service.RoleService.userService)
↑     ↓
|  userService (field private com.bjowernode.springbootcircledependency.service.RoleService com.bjowernode.springbootcircledependency.service.UserService.roleService)
└─────┘


Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

五、解决循环依赖报错

5.1 方法一,修改application.yml配置文件

在application.yml配置文件中,配置如下内容即可

spring:
  main:
    allow-circular-references: true

再次运行启动类,就没有报错了

在这里插入图片描述

5.2 方法二:将成员变量转化为方法调用(引入依赖的方式)

5.2.1 pom.xml 中 引入依赖

 <dependency>
            <groupId>xin.altitude.cms</groupId>
            <artifactId>ucode-cms-common</artifactId>
            <version>1.6.2</version>
        </dependency>

5.2.2 将成员变量转化为方法调用

5.2.2.1 RoleService
package com.bjpowernode.springbootcircledependency02.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xin.altitude.cms.common.util.SpringUtils;

@Service
public class RoleService {

    /**
     * 将成员变量转化为方法调用
     */
    private UserService userService(){
        UserService userService = SpringUtils.getBean(UserService.class);
        return userService;

    }

}

5.2.2.2 UserService
package com.bjpowernode.springbootcircledependency02.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xin.altitude.cms.common.util.SpringUtils;

@Service
public class UserService {

    /**
     * 将成员变量转化为方法调用
     */
    private RoleService roleService(){
        RoleService  roleService= SpringUtils.getBean(RoleService.class);
        return roleService;
    }
}

5.2.3 运行启动类

不报错了
在这里插入图片描述

5.3 方法三:将成员变量转化为方法调用(SpringUtils工具类的方式)

5.3.1 SpringUtils工具类

package com.bjpowernode.springbootcircledependency03.utils;

import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring工具类 方便在非spring管理环境中获取bean
 *
 * @author jianyongchao
 */
@Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware
{
    /** Spring应用上下文环境 */
    private static ConfigurableListableBeanFactory beanFactory;

    private static ApplicationContext applicationContext;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
    {
        SpringUtils.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        SpringUtils.applicationContext = applicationContext;
    }

    /**
     * 获取对象
     *
     * @param name
     * @return Object 一个以所给名字注册的bean的实例
     * @throws org.springframework.beans.BeansException
     *
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException
    {
        return (T) beanFactory.getBean(name);
    }

    /**
     * 获取类型为requiredType的对象
     *
     * @param clz
     * @return
     * @throws org.springframework.beans.BeansException
     *
     */
    public static <T> T getBean(Class<T> clz) throws BeansException
    {
        T result = (T) beanFactory.getBean(clz);
        return result;
    }

    /**
     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
     *
     * @param name
     * @return boolean
     */
    public static boolean containsBean(String name)
    {
        return beanFactory.containsBean(name);
    }


}

5.3.2 将成员变量转化为方法调用

5.3.2.1 RoleService
package com.bjpowernode.springbootcircledependency02.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xin.altitude.cms.common.util.SpringUtils;

@Service
public class RoleService {

    /**
     * 将成员变量转化为方法调用
     */
    private UserService userService(){
        UserService userService = SpringUtils.getBean(UserService.class);
        return userService;

    }

}
5.3.2.2 UserService
package com.bjpowernode.springbootcircledependency02.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xin.altitude.cms.common.util.SpringUtils;

@Service
public class UserService {

    /**
     * 将成员变量转化为方法调用
     */
    private RoleService roleService(){
        RoleService  roleService= SpringUtils.getBean(RoleService.class);
        return roleService;
    }
}

5.4 方法四 抽取成公共的服务类

CommonService.java

package com.bjpowernode.springbootcircledependency04.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommonService {
    @Autowired
    private RoleServcice roleServcice;
    @Autowired
    private UserService userService;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/356535.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

React SSR

ReactDOMServer 参考链接&#xff1a;https://zh-hans.reactjs.org/docs/react-dom-server.html ReactDOMServer 对象允许你将组件渲染成静态标记。通常&#xff0c;它被使用在 Node 服务端上 // ES modules import * as ReactDOMServer from react-dom/server; // CommonJS v…

全栈之路-前端篇 | 第二讲.基础前置知识【应用服务端与编程语言】学习笔记

欢迎关注「全栈工程师修炼指南」公众号 点击 &#x1f447; 下方卡片 即可关注我哟! 设为「星标⭐」每天带你 基础入门 到 进阶实践 再到 放弃学习&#xff01; 涉及 企业运维、网络安全、应用开发、物联网、人工智能、大数据 学习知识 “ 花开堪折直须折&#xff0c;莫待无花…

谓词exsits用法及与in的使用区别

exists用法 大白话的说&#xff0c;exists的执行&#xff0c;是依次拿外层表的每条记录&#xff0c;去和exsits后的子查询表按你所定义的运算规则&#xff08;如果有的话&#xff09;做运算&#xff0c;如果存在结果&#xff0c;也就是有返回数据&#xff0c;无论这部分数据有…

MySQL中使用索引优化

目录 一.使用索引优化 数据准备 避免索引失效应用-全值匹配 避免索引失效应用-最左前缀法则 避免索引失效应用-其他匹配原则 1、 2、 3、 4、 5、 一.使用索引优化 索引是数据库优化最常用也是最重要的手段之一,通过索引通常可以帮助用户解决大多数的MySQL的性能优化…

Pandas库入门仅需10分钟

数据处理的时候经常性需要整理出表格&#xff0c;在这里介绍pandas常见使用&#xff0c;目录如下&#xff1a; 数据结构导入导出文件对数据进行操作 – 增加数据&#xff08;创建数据&#xff09; – 删除数据 – 改动数据 – 查找数据 – 常用操作&#xff08;转置&#xff0…

Linux 配置RAID组

目录 配置RAID&#xff08;软件RAID&#xff09; 创建RAID组 RAID中出现坏盘如何操作 RAID 添加热备盘 删除RAID组 RAID所解决的问题 提升硬盘的I/O吞吐率 提高硬盘的读写能力 提高硬盘的安全性 进行备份 减少硬盘成本 RAID级别 存储RAID——RAID级别_静下心来敲木鱼的博…

Spring Boot中使用@Autowire装配接口是怎么回事?

在学习使用Spring Boot框架时候&#xff0c;发现了一个特别的现象UserMapper是一个接口&#xff0c;在另一个类中好像直接使用Autowired装配了一个UserMapper对象&#xff1f;&#xff1f;&#xff1f;我纳闷了一会儿&#xff0c;接口居然可以直接实例对象吗&#xff1f;根据我…

测试开发之Django实战示例 第十三章 上线

在上一章&#xff0c;为其他程序与我们的Web应用交互创建了RESTful API。本章将学习如何创建生产环境让我们的网站正式上线&#xff0c;主要内容有&#xff1a;配置生产环境创建自定义中间件实现自定义管理命令1创建生产环境现在该将Django项目正式部署到生产环境中了。我们将按…

深度解读依赖注入DI源码

spring-framework-5.3.10 版本依赖注入代码的入口在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean 。如果想搞清楚为什么是这里需要去学习下Bean的生命周期&#xff0c;这里就不介绍了。AutowiredAutowired private OrderServic…

电子科技大学操作系统期末复习笔记(五):文件管理

目录 前言 文件管理&#xff1a;基础 基本概念 文件 文件系统 文件系统的实现模型 文件的组成 文件名 文件分类 文件结构 逻辑结构 物理结构 练习题 文件管理&#xff1a;目录 文件控制块FCB FCB&#xff1a;File Control Block FCB信息 目录 基本概念 目…

CAN通信笔记-位时间、Tq及采样点同步

本文框架1.前言2. 位时间2.1 位时间定义2.2 位时间计算3. Tq3.1 Tq的计算3.1.1 举个例子3.2 位时间与Tq的换算4. 采样点同步4.1 硬同步4.2 重同步4.2.1 延长PBS1的重同步4.2.2 缩短PBS2的重同步1.前言 本篇记录些关于CAN的一些学习笔记&#xff0c;说实话CAN协议发展的已经非常…

【项目设计】—— 负载均衡式在线OJ平台

目录 一、项目的相关背景 二、所用技术栈和开发环境 三、项目的宏观结构 四、compile_server模块设计 1. 编译服务&#xff08;compiler模块&#xff09; 2. 运行服务&#xff08;runner模块&#xff09; 3. 编译并运行服务&#xff08;compile_run模块&#xff09; 4…

MicroBlaze系列教程(6):AXI_IIC的使用(24C04 EEPROM)

文章目录 @[toc]AXI_IIC简介MicroBlaze硬件配置常用函数使用示例波形实测参考资料工程下载本文是Xilinx MicroBlaze系列教程的第6篇文章。 AXI_IIC简介 一般情况下,使用FPGA实现I2C协议主要有两种方式:一种是基于Verilog实现起始位、停止位、ACK产生和判断、数据的发送和接收…

1.HTTP及Template介绍

目录 来源 介绍 模板与渲染 Go语言的模板引擎 模板引擎的使用 定义模板文件 解析模板文件 模板渲染 基本示例 模板语法 {{.}} 注释 pipeline 变量 移除空格 条件判断 range with 预定义函数 比较函数 自定义函数 模板的嵌套template block 修改默认的标…

加油站会员管理小程序实战开发教程13

我们上一篇讲解了会员注册的功能,本篇我们介绍一下会员开卡的功能。 会员注册之后,可以进行开卡的动作。一个会员可以有多张会员卡,在微搭中用来描述这种一对多的关系的,我们用关联关系来表达。 登录微搭的控制台,点击数据模型,点击新建数据模型 输入数据源的名称会员卡…

基于 Debain11 构建 asp.net core 6.x 的基础运行时镜像

基于 Debain11 构建 asp.net core 6.x 的基础运行时镜像Linux 环境说明Debian 简介Debian 发行版本关于 Debian 11Linux 常用基础工具Dockerfile 中 RUN 指令RUN 语法格式RUN 语义说明编写 Dockerfile 构建 Runtime 基础镜像ASP.NET Core Runtime 基础镜像Dockerfile 编写Windo…

sklearn主成分分析PCA

文章目录基本原理PCA类图像降维与恢复基本原理 PCA&#xff0c;即主成分分析(Principal components analysis)&#xff0c;顾名思义就是把矩阵分解成简单的组分进行研究&#xff0c;而拆解矩阵的主要工具是线性变换&#xff0c;具体形式则是奇异值分解。 设有mmm个nnn维样本X…

【面试题】数组reduce的用法

1. 对数组求和 传统的数组求和方式需要使用forEach循环遍历数组中的每一个元素&#xff0c;然后累加。然而这种方式需要新增一个用于存储累加结果的变量。 function sum(arr) {let res 0arr.forEach(element > res res element);return res }还可以通过数组的reduce方法…

算法笔记(九)—— 暴力递归

暴力递归&#xff08;尝试&#xff09; 1. 将问题转化为规模缩小了的同类问题子问题 2. 有明确的不需要的继续递归的条件 3. 有当得到子问题结果之后的决策过程 4. 不记录每一个子问题的解 Question&#xff1a;经典汉诺塔问题 1. 理解清楚&#xff0c;基础三个圆盘的移动…

2.5|iot|嵌入式Linux系统开发与应用|第4章:Linux外壳shell脚本程序编程

1.shell基础 Shell是Linux操作系统内核的外壳&#xff0c;它为用户提供使用操作系统的命令接口。 用户在提示符下输入的每个命令都由shell先解释然后发给Linux内核&#xff0c;所以Linux中的命令通称为shell命令。 通常我们使用shell来使用Linux操作系统。Linux系统的shell是…