今天在做调整SpringBoot项目,项目一直启动不起来,通过查看日志看到如下报错:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'thirdPartnerInteceptor': Unsatisfied dependency expressed through field 'cacheUtils'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cacheUtils': Unsatisfied dependency expressed through field 'customerService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.demo.api.customer.facade.CustomerService': FactoryBean threw exception on object creation; 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?
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
... 88 common frames omitted
看报错应该是Bean注入时有异常,导致注入失败,经过分析是项目的拦截器里面有一个Bean注入导致的,在拦截器中有一个缓存工具类需要调用第三方服务,而第三方服务接口已经通过OpenFeign进行了封装,在工具类中注入这个OpenFeign的服务时提示注入失败,这可能与加载顺序有关,后来调整为懒加载方式 @Lazy 项目能够启动,先记下调整内容有时间在研究一下原理,客户服务类的方法封装如下:
/**
* 客户信息服务
*/
@Component
@FeignClient(contextId = "customerService", name = "API-CUSTOMER-SERVER", path = "/customer", fallbackFactory = CustomerService.CustomerServiceImpl.class)
public interface CustomerService {
/**
* 根据客户ID获取客户信息
* @param customerId 客户ID
* @return
*/
@GetMapping("/find/byid/{cid}")
Result<CustomerResp> findCustomerById(@PathVariable("cid") long customerId);
@Component
public static class CustomerServiceImpl implements FallbackFactory<CustomerService> {
@Override
public CustomerService create(Throwable cause) {
cause.printStackTrace();
return new CustomerService() {
@Override
public Result<CustomerResp> findCustomerById(long customerId) {
return Result.fail(StatusCode.C_ERROR).setMessage("客户服务请求失败,请稍后再试");
}
};
}
}
}
调整注入方式如下:
@Component
public class CacheUtils {
@Autowired
@Lazy
private CustomerService customerService;
...其他处理代码
}