首页 > 解决方案 > Spring Boot 错误 org.springframework.beans.factory.UnsatisfiedDependencyException

问题描述

org.springframework.beans.factory.UnsatisfiedDependencyException:创建文件 [C:\Users\guptadee\Projects\Server\Vista\docstash_api_migration\target\classes\com\adp\avs\tax\form 中定义的名称为“producer”的 bean 时出错\DocStashElasticMigration\Service\Producer.class]:通过构造函数参数0表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“java.util.concurrent.BlockingQueue”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:{} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework .beans.factory.support.ConstructorResolver。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。依赖注解:{} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework .beans.factory.support.DefaultListableBeanFactory。

迁移.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");

    }

}

生产者.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) {

        }

    }
}

标签: javaspring-boot

解决方案


消息很清楚:找不到类型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<OutputDocument> q,因此是例外。

您需要提供该类型的 bean:

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

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

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

如果你声明你的Produceras @Component,你应该将它注入你的Migration类而不是手动创建它。


推荐阅读