您现在的位置是: 首页 > 后端开发 > Java SpringBoot 启动提示 Requested bean is currently in creation: Is there an unresolvable circular reference?

SpringBoot 启动提示 Requested bean is currently in creation: Is there an unresolvable circular reference?

2020-09-15 Java 2108人已围观 4260次浏览

简介SpringBoot 启动提示 Requested bean is currently in creation: Is there an unresolvable circular reference?

在项目开发过程中,涉及到用户类读取相关产品,以及产品类需要读取用户信息的操作,所以在 UserService 和 ProductService 类中分别引用了对方,导致在 SpringBoot 启动的过程中出现 Requested bean is currently in creation: Is there an unresolvable circular reference? 提示,并且在报错信息中,提示了循环依赖的报错

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

   mgrNoticeController defined in file [/....../api/target/classes/com/yujia/topbang/api/controller/mgr/NoticeController.class]
      ↓
   noticeSendServiceImpl defined in file [/....../api/target/classes/com/yujia/topbang/api/service/impl/NoticeSendServiceImpl.class]
┌─────┐
|  userServiceImpl defined in file [/....../api/service/impl/UserServiceImpl.class]
↑     ↓
|  productServiceImpl defined in file [/....../api/service/impl/ProductServiceImpl.class]
└─────┘

启动错误提示中,已经说明了。userServiceImpl 和 productServiceImpl 存在循环依赖,相当于就是 user 里面要去调用 product , product 里面又调用了 user ,导致死循环之类的问题

出现此类错误,解决方案只能是删除循环依赖的地方,但是实际业务中,又需要用户调用产品,产品获取用户信息,这样的情况下,目前我的解决方案就是新建一个公用的类,比如 UserProductService 这种,里面依赖 UserService 和 ProductService ,这样在使用的过程中,用户是用户,产品是产品,同时使用的时候就在 UserProductService 中操作,这样就可以避免循环依赖的问题

很赞哦! (0)

站长推荐

站点信息

  • 网站地图