SpringBoot中懒加载解决注入问题

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。SpringBoot中懒加载解决注入问题,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

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

	...其他处理代码
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/181872.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!