Spring Boot Error org.springframework.beans.factory.UnsatisfiedDependencyException

时间:2018-05-14 16:38:42

标签: java spring-boot

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为' producer'的bean时出错在文件[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个豆有资格作为autowire候选者。依赖注释:{}     在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个豆有资格作为autowire候选者。依赖注释:{}     在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个常见帧

  

Migration.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) {

        }

    }
}

2 个答案:

答案 0 :(得分:1)

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

@Bean
public BlockingQueue<OutputDocument> createBlockingQueue() {

}

答案 1 :(得分: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<OutputDocument> q因此异常。

您需要提供该类型的bean:

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

提供此bean之后,spring也会抱怨yearqtr。所以你必须为它提供这些bean。 的修改

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

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