设置 @Bean MybatisPlusInterceptor 后启动时出现 Requested bean is currently in creation: Is there an unresolvable circular reference? 问题如何解决
时间: 2023-03-15 19:21:32 浏览: 244
这个问题可能是由于MybatisPlusInterceptor Bean未正确配置导致的。您可以检查MybatisPlusInterceptor Bean是否配置正确,以及Bean之间是否存在循环引用。如果在检查过程中发现问题,您可以尝试重新配置MybatisPlusInterceptor Bean,或者尝试修改Bean之间的引用关系。
相关问题
Requested bean is currently in creation: Is there an unresolvable circular reference?
This error message is usually caused by a circular dependency between beans in Spring framework. It means that two or more beans are referring to each other directly or indirectly, which creates a circular reference and causes Spring to fail to construct the beans.
To solve this issue, you can try one or more of the following:
1. Refactor your code to remove the circular dependency between beans.
2. Use setter-based injection instead of constructor-based injection.
3. Use @Lazy annotation to delay the initialization of one of the beans.
4. Use @Autowired with @Qualifier to specify which bean to inject.
It's important to note that circular dependencies could indicate a design flaw or a violation of the Single Responsibility Principle, so it's always recommended to review your code and make sure that the circular dependency is really necessary before trying to solve it.
手动解决 Requested bean is currently in creation: Is there an unresolvable circular reference?
这个问题通常出现在Spring框架中,当你试图注入一个正在创建中的bean时,会抛出"Requested bean is currently in creation: Is there an unresolvable circular reference?"异常。这是因为在依赖注入过程中,如果存在循环依赖(即A依赖B,B又依赖A或者其他互相依赖的情况),Spring容器在尝试构建依赖图时无法确定bean的完整生命周期顺序,导致了无限递归。
手动解决这个问题有几种方法:
1. **修改依赖关系**:检查并重构代码,移除或打破循环依赖。通常是通过改变依赖的方式,如将共享服务抽取成独立的服务,或者调整模块之间的职责划分。
2. **延迟加载**:对于那些非必需的、可以延迟初始化的循环依赖,可以设置`@Lazy(true)`注解,让Spring在真正需要的时候才去创建对应的bean。
3. **启用构造函数注入**:如果可能,尽量避免使用setter注入,改用构造函数注入,因为Spring默认是通过setters来管理bean的依赖关系,这可能会更容易引发循环依赖。
4. **使用`@ConditionalOnMissingBean`**:当检测到某个bean不存在时再注入,可以防止创建循环依赖。
阅读全文