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

问题产生原因

  1. A类引入B类
  2. B类引入A类
  3. 造成Spring Bean的循环依赖

解决方案看下面

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-12-22 20:28:03.186 ERROR 1492 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter - 

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

Description:

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

   mallSalesController defined in file [D:\Data\GitLab\badger-brand-cloud\badger-brand-admin\badger-brand-admin-server\target\classes\com\badger\controller\MallSalesController.class]
┌─────┐
|  mallSalesServiceImpl defined in file [D:\Data\GitLab\badger-brand-cloud\badger-brand-admin\badger-brand-admin-server\target\classes\com\badger\service\impl\MallSalesServiceImpl.class]
↑     ↓
|  sysBrandMallServiceImpl defined in file [D:\Data\GitLab\badger-brand-cloud\badger-brand-admin\badger-brand-admin-server\target\classes\com\badger\service\impl\SysBrandMallServiceImpl.class]
└─────┘


Disconnected from the target VM, address: '127.0.0.1:65479', transport: 'socket'

Process finished with exit code 1

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

解决方案

  1. A 类继续使用 @Autowired注入
  2. B类使用 @Lazy 注解加 set注入

示例:A类

@Service
public class AServiceImpl implements AUserService{

    private final BUserService service;

    public AServiceImpl (@Lazy BUserService service ) {
        this.service = service ;
    }

示例:B类

@Service
public class AServiceImpl implements BUserService{

    private final AUserService service;

    public BServiceImpl (@Lazy AUserService service) {
        this.service = service;
    }
上一篇:Redis持久化策略


下一篇:使用jquery-validate插件表单验证