Spring Boot 错误 org.springframework.beans.factory.UnsatisfiedDependencyException

Posted

技术标签:

【中文标题】Spring Boot 错误 org.springframework.beans.factory.UnsatisfiedDependencyException【英文标题】:Spring Boot Error org.springframework.beans.factory.UnsatisfiedDependencyException 【发布时间】:2018-10-24 09:35:34 【问题描述】:

org.springframework.beans.factory.UnsatisfiedDependencyException: 在文件 [C:\Users\guptadee\Projects\Server\Vista\docstash_api_migration\target\classes\com\adp\avs\ tax\form\DocStashElasticMigration\Service\Producer.class]:通过构造函数参数0表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“java.util.concurrent.BlockingQueue”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注释: 在 org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1201) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1103) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE] 在 com.adp.avs.tax.form.DocStashElasticMigration.DocStashElasticMigrationApplication.main(DocStashElasticMigrationApplication.java:29) [classes/:na] 引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“java.util.concurrent.BlockingQueue”类型的合格bean:预计至少有1个有资格作为自动装配候选者的bean。依赖注释: 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] 在 org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] ...省略了18个常用框架

迁移.java

@Service
public class Migration 

    @Autowired
    private TransportClient transportClient;

    @Autowired
    ClientDao clientDao;

    @Value("#'$quarterly.form'.split(',')")
    private List<String> quarterlyForm;

    @Value("#'$yearly.form'.split(',')")
    private List<String> yearlyForm;

    @Value("$threadCount:5")
    private int threadCount;


    private final String oldIndex = "taxdocument";

    private final String newTaxformIndex = "";

    private final String newDocumentIndex = "";

    public static String type = "document";


    public void migratetoNewIndex(int year, int qtr) throws Exception
        //Creating BlockingQueue of size 2000
        BlockingQueue<OutputDocument> queue = new ArrayBlockingQueue<OutputDocument>(2000);
        final Map<String,List<OrganizationUnit>> organizationUnitMap = clientDao.getAllOrg(String.valueOf(year).substring(2), String.valueOf(qtr));
        Producer producer = new Producer(queue,year,qtr);
        Thread producerThread = new Thread(producer);
        producerThread.start();
        List<Thread> consumerList = new ArrayList<Thread>();
        for(int i=0;i<threadCount;i++) 
            Consumer consumer = new Consumer(queue, organizationUnitMap);
            Thread T1 = new Thread(consumer);
            T1.start();
            consumerList.add(T1);
        
        for (int i = 0; i < consumerList.size(); i++) 
            consumerList.get(i).join();
        
        System.out.println("Producer and Consumer has been started");

    


Producer.java

@Component
public class Producer implements Runnable 

    @Autowired
    private TransportClient transportClient;

    @Value("#'$quarterly.form'.split(',')")
    private List<String> quarterlyForm;

    @Value("#'$yearly.form'.split(',')")
    private List<String> yearlyForm;

    @Value("$threadCount:5")
    private int threadCount;

    private final String oldIndex = "taxdocument";

    private final String type = "document";

    private int year;

    private int qtr;


    private BlockingQueue<OutputDocument> queue;

    public Producer(BlockingQueue<OutputDocument> q, int year,int qtr) 
        this.queue = q;
        this.year=year;
        this.qtr=qtr;

    

    @Override
    public void run() 

        List subcatecory = new ArrayList();
        subcatecory.addAll(quarterlyForm);
        try 
            List<String> yearQtr = new ArrayList<String>();
            yearQtr.add(year + "/" + qtr);
            if (qtr == 4) 
                subcatecory.addAll(yearlyForm);
                yearQtr.add(String.valueOf(year));
            
            SearchResponse scrollResp = transportClient.prepareSearch(oldIndex)
                    .setTypes(type)
                    .addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
                    .setScroll(new TimeValue(600000))
                    .setSize(1000)
                    .setQuery(boolQuery().must(QueryBuilders.termsQuery("subCategoryCode.codeValue", subcatecory))
                            .must(QueryBuilders.termsQuery("applicationData.yearQuarter", yearQtr)))
                    .get(); //max of 100 hits will be returned for each scroll

            if (scrollResp.getHits().getTotalHits() > 0) 
                OutputDocument outputDocument = null;
                Map<String, Object> responseMap = new HashMap<String, Object>();
                ObjectMapper mapper = new ObjectMapper();
                List<OutputDocument> documentList = new ArrayList<OutputDocument>();
                do 
                    for (SearchHit hit : scrollResp.getHits().getHits()) 
                        responseMap = hit.sourceAsMap();
                        responseMap.remove("storeKey");
                        outputDocument = mapper.convertValue(responseMap, OutputDocument.class);
                        queue.put(outputDocument);
                    
                    scrollResp = transportClient.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
                 while (scrollResp.getHits().getHits().length != 0); // Zero hits mark the end of the scroll and the while loop.

                //exit message to child threads
                for(int i=0; i<threadCount; i++) 
                    OutputDocument exitMessage = new OutputDocument();
                    exitMessage.setItemID("exit");
                    queue.put(exitMessage);
                
            

         catch (Exception e) 

        

    

【问题讨论】:

【参考方案1】:

消息很清楚:找不到BlockingQueue 类型的bean。

你将你的类注释为

@Component
public class Producer implements Runnable

因此 Spring 将尝试初始化这种类型的单例 bean。

当你声明一个构造函数时:

public Producer(BlockingQueue<OutputDocument> q, int year,int qtr) 
    this.queue = q;
    this.year=year;
    this.qtr=qtr;


因此 Spring 将尝试使用该构造函数来初始化 bean。

但没有找到第一个参数BlockingQueue&lt;OutputDocument&gt; q,因此出现异常。

您需要提供该类型的 bean:

@Bean
BlockingQueue<OutputDocument> createBlockingQueue() 
...

提供这个bean后,spring也会抱怨yearqtr。所以你必须为它提供那些bean。 编辑

您的依赖管理存在严重问题。

如果您将 Producer 声明为 @Component,则应将其注入您的 Migration 类,而不是手动创建它。

【讨论】:

【参考方案2】:

参见公共 Producer(BlockingQueue q, int year,int qtr) 您没有声明类型的工厂实例限定 bean 'java.util.concurrent.BlockingQueue' 阻塞队列 您可以添加工厂方法或工厂 bean

@Bean
public BlockingQueue<OutputDocument> createBlockingQueue() 


【讨论】:

以上是关于Spring Boot 错误 org.springframework.beans.factory.UnsatisfiedDependencyException的主要内容,如果未能解决你的问题,请参考以下文章

SpringBootspringProject‘org.springframework.boot:spring-boot-starter-parent:2.x.x‘ not found

Spring Boot 配置详解

如何搭建spring boot

spring boot 打印myabtis sql语句

spring boot 怎么实现集群

Spring Boot 2.0:使用 Docker 部署 Spring Boot