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;
}


















