SpringBoot中懒加载解决注入问题

今天在做调整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;

	...其他处理代码
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值