有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么一个假客户端创建了对WebMvcAutoConfiguration的循环依赖关系?

我有以下设置

@Configuration
@ConditionalOnProperty(name = "my.enabled", havingValue = "true", matchIfMissing = true)
public class MyMvcConfigurer implements WebMvcConfigurer {
    private final MyInterceptor myInterceptor;
    ...
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor);
    }
}
@Component
@ConditionalOnProperty(name = "my.enabled", havingValue = "true", matchIfMissing = true)
public class MyInterceptor implements HandlerInterceptor {
    private final MyService myService;

    public MyInterceptor(MyService myService) {
        this.myService = myService;
    }

@Service
@ConditionalOnProperty(name = "my.enabled", havingValue = "true")
public class MyServiceImpl implements MyService {
    private final MyClient myClient;
//    private final ObjectProvider<MyClient> myClient; <-- This does work
@FeignClient
@ConditionalOnProperty(name = "my.enabled", havingValue = "true")
public interface MyClient {
...
}

这将导致以下错误:

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

┌─────┐
|  myWebMvcConfigurer
↑     ↓
|  myProtectedInterceptor
↑     ↓
|  myServiceImpl
↑     ↓
|  org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration
└─────┘

当我在服务实现中切换到private final ObjectProvider<MyClient> myClient;时,它就会工作

为什么会这样?我不确定服务实现如何强制依赖自定义WebMVCConfiguer。为什么只有当假客户端在类中时才会发生


共 (0) 个答案