栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Feign加载异常:Requested bean is currently in creation: Is there an unresolvable circular reference

Feign加载异常:Requested bean is currently in creation: Is there an unresolvable circular reference

现象

        在使用EnableFeignClients指定加载包时,启动出现下面的异常。

ERROR 56739 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 
'org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration': 

Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is 
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'webConfigurerAdapter':

Unsatisfied dependency expressed through field 'cipherAuthInterceptor'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:

Error creating bean with name 'cipherAuthInterceptor': 
Unsatisfied dependency expressed through field 'authService';

nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
 Error creating bean with name 'mvcResourceUrlProvider': 
Requested bean is currently in creation: Is there an unresolvable circular reference?
原因分析 本例相关代码

启动类

@SpringBootApplication
@EnableFeignClients({"com.lizz.test.api"})
public class LizzApplication {
    public static void main(String[] args) {
        SpringApplication.run(LizzApplication.class, args);
    }
}
package com.lizz.test.api;
@FeignClient(name = "LIZZ-TEST-SERVER")
public interface AuthService {
    
    @PostMapping("/auth")
    Boolean checkUri(@RequestParam(name = "token") String token,
                    @RequestParam(name = "uri") String uri);
}

配置类添加拦截器

@Configuration
public class WebConfigurerAdapter implements WebMvcConfigurer {

    @Autowired
    private AuthInterceptor authInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //增加自定义拦截方法
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("
    @PostMapping("/test")
    Boolean test();
}

 引用其他Feign client

@Service
public class LizzServiceImpl  {

    @Autowired
    private LizzOtherServer lizzOtherServer;

    public void test(String type, String data) {
       lizzOtherServer.test();
    }
}

使用了WebMvcConfigurer添加拦截器又在HandlerInterceptor拦截器中调用了feignclient服务因此出现了循环引用的情况如果只有这一个地方加载了feignclient服务,也不会出现异常情况。由于本例中在其他service中使用了feignclinet方法,FeignClient初始化时会将所有feignclient进行初始化。导致在HandlerInterceptor加载时出现了异常。 解决办法

出现类是循环引用时,通过@Lazy 延时加载的方式解决即service类中加载其他feignclinet时,不加载HandlerInterceptor中引用的类如果service中的类和HandlerInterceptor一样,都可以加上@Lazy

@Slf4j
@Configuration
public class AuthInterceptor implements HandlerInterceptor {
    @Autowired
    @Lazy
    private AuthService authService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        if (authService.checkUri("xxx","xxx")) {
            return true;
        } else {
            return false;
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/707577.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号