1
        public class TransactionHistoryCsvItemProcessor  implements ItemStream, ItemProcessor<TransactionHistory,TransactionHistory>{
            @Autowired
            private TransactionHistoryRepository transactionHistoryRepository;
            @Autowired
            private ProductHierarchyRepository productHierarchyRepository;
            @Autowired
            private LocationHierarchyRepository locationHierarchyRepository;


            List<TransactionHistory>list= transactionHistoryRepository.findAll();
            List<String> pLevel7Products = productHierarchyRepository.getPLevel7Ids();
            List<String> level5locations=locationHierarchyRepository.getLevel5Ids();


            int count=1;

            TransactionHistoryCsvItemProcessor(){

            }


            private Set<TransactionHistory> processedData = new HashSet<TransactionHistory>();
            @Override
            public TransactionHistory process(TransactionHistory transactionHistory) throws Exception {

                TransactionHistory processedObj = new TransactionHistory(); 

                    if (pLevel7Products.contains(transactionHistory.getProductId().trim())) {

                        if (level5locations.contains(transactionHistory.getLocationId().trim())) {

                            if(list!=null&&!list.isEmpty()){
                                 if(list.contains(transactionHistory)){
                                     count++;
                                     throw new MyOwnException(" duplicates data error", transactionHistory.getProductId().trim(),
                                                transactionHistory.getLocationId().trim(), transactionHistory.getPeriodId(),
                                                transactionHistory.getQuantity(), "at line numer ", count-1);
                                }
                            }

                                      processedObj.setProductId(transactionHistory.getProductId().trim());
                                        processedObj.setLocationId(transactionHistory.getLocationId().trim());
                                        processedObj.setQuantity(transactionHistory.getQuantity());
                                        processedObj.setPeriodId(transactionHistory.getPeriodId());
                                        processedObj.setCreatedDate(LocalDate.now());
                                        processedData.add(transactionHistory);
                                        transactionHistory.setItemCount(count);
                                        count++;

                        }else {
                            System.out.println("failed location");
                            count++;
                            transactionHistory.setItemCount(count);
                            throw new MyOwnException(" location data error", transactionHistory.getProductId().trim(),
                                    transactionHistory.getLocationId().trim(), transactionHistory.getPeriodId(),
                                    transactionHistory.getQuantity(), "at line numer ", count-1);
                        }
                    } else {
                        System.out.println("failed product");
                        // count++;
                        try {
                            transactionHistory.setItemCount(count);
                            throw new MyOwnException(" product data error", transactionHistory.getProductId().trim(),
                                    transactionHistory.getLocationId().trim(), transactionHistory.getPeriodId(),
                                    transactionHistory.getQuantity(), "at line numer ", count);
                        } catch (MyOwnException e) {
                            System.out.println("product error");
                            throw new MyOwnException(" product data error", transactionHistory.getProductId().trim(),
                                    transactionHistory.getLocationId().trim(), transactionHistory.getPeriodId(),
                                    transactionHistory.getQuantity(), "at line numer ", count);

                        } finally {
                            count++;
                        }
                    }

                //}

                if (processedObj.getProductId() == null && processedObj.getLocationId() == null)
                    return null;
                else {
                    // processedData.add(processedObj);
                    return processedObj;
                }
            }

            @Override
            public void open(ExecutionContext executionContext) throws ItemStreamException {
                // TODO Auto-generated method stub
                //list=transactionHistoryRepository.findAll();
            }
            @Override
            public void update(ExecutionContext executionContext) throws ItemStreamException {
                // TODO Auto-generated method stub  
            }
            @Override
            public void close() throws ItemStreamException {
                // TODO Auto-generated method stub
                count=1;    
            }
        }
    //============================================//
    @Bean
        public Step transactionHistoryStep() {
            return  ((SimpleStepBuilder<TransactionHistory, TransactionHistory>) stepBuilderFactory.get("transactionHistoryStep")
                    .<TransactionHistory,TransactionHistory>chunk(10)
                    .reader(reader())
                    .processor(processor())
                    .writer(writer())
                    .stream(new TransactionHistoryCsvItemProcessor()))
                    .faultTolerant()
                    .skipPolicy(transactionHistoryCsvImportSkipPolicy)
                    .build();

        }


        @Bean
        @JobScope
        public FlatFileItemReader<TransactionHistory> reader() {
            FlatFileItemReader<TransactionHistory> flatFileItemReader= new FlatFileItemReader<TransactionHistory>();
            try {

                TransactionHistoryFieldSetMapper transactionHistoryFieldSetMapper= new TransactionHistoryFieldSetMapper();

                flatFileItemReader.setResource(new FileSystemResource(FileResources.mappingFileResouces("transactionHistoryImportCsvFile")));
                flatFileItemReader.setName("CSV-Reader");
                flatFileItemReader.setLinesToSkip(1);
                flatFileItemReader.setLineMapper(new DefaultLineMapper<TransactionHistory>(){{
                    setLineTokenizer(new DelimitedLineTokenizer() {{
                        setNames("productId","locationId","periodId","quantity");
                        setFieldSetMapper(transactionHistoryFieldSetMapper);

                    }});
                    setFieldSetMapper(new TransactionHistoryFieldSetMapper(){{
                        //setTargetType(TransactionHistory.class);

                    }});
                }});
            }
            catch(Exception e) {
                e.printStackTrace();
                logger.error("read error"+flatFileItemReader);
            }

            return flatFileItemReader;
        }


         @Bean
         public ItemProcessor<TransactionHistory, TransactionHistory> processor() {
               return new TransactionHistoryCsvItemProcessor();
            }

//===========error ====//

启动 ApplicationContext 时出错。要显示条件报告,请在启用“调试”的情况下重新运行您的应用程序。2020-May-05 22:30:13.654 错误 [main] osbSpringApplication - 应用程序运行失败 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“transactionHistoryController”的 bean 时出错:通过字段“transactionHistoryCsvImportJob”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/datalabsindia/batch/TransactionHistoryCsvImport.class] 中定义名称为“transactionHistoryJob”的 bean 创建错误:通过工厂方法进行 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.batch.core.Job]:工厂方法“transactionHistoryJob”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/datalabsindia/batch/TransactionHistoryCsvImport.class] 中定义名称为“transactionHistoryStep”的 bean 创建错误:通过工厂方法实例化 bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.batch.core.Step]:工厂方法“transactionHistoryStep”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/datalabsindia/batch/TransactionHistoryCsvImport.class] 中定义名称为“处理器”的 bean 创建错误:通过工厂方法进行的 Bean 实例化失败;嵌套异常是 org.springframework.beans。BeanInstantiationException:无法实例化 [org.springframework.batch.item.ItemProcessor]:工厂方法“处理器”抛出异常;嵌套异常是 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java: 90) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) 在 org.springframework.beans 的 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) .factory.support.AbstractAutowireCapableBeanFactory。org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/datalabsindia/batch/TransactionHistoryCsvImport.class] 中定义名称为“transactionHistoryJob”的 bean 创建错误:通过工厂方法进行 bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.batch.core.Job]:工厂方法“transactionHistoryJob”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/datalabsindia/batch/TransactionHistoryCsvImport.class] 中定义名称为“transactionHistoryStep”的 bean 创建错误:通过工厂方法实例化 bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.batch。core.Step]:工厂方法“transactionHistoryStep”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/datalabsindia/batch/TransactionHistoryCsvImport.class] 中定义名称为“处理器”的 bean 创建错误:通过工厂方法进行的 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.batch.item.ItemProcessor]:工厂方法“处理器”抛出异常;嵌套异常是 org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456) 的 org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) 的 java.lang.NullPointerException在 org.springframework。springframework.batch.core.Job]:工厂方法“transactionHistoryJob”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/datalabsindia/batch/TransactionHistoryCsvImport.class] 中定义名称为“transactionHistoryStep”的 bean 创建错误:通过工厂方法进行 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.batch.core.Step]:工厂方法“transactionHistoryStep”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/datalabsindia/batch/TransactionHistoryCsvImport.class] 中定义名称为“处理器”的 bean 创建错误:通过工厂方法进行的 bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.batch.item.ItemProcessor]:工厂方法“处理器”抛出异常;嵌套异常是 org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) 处的 java.lang.NullPointerException org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ...省略了 32 个常见框架 原因:org.springframework.beans.factory.BeanCreationException:创建类路径资源 [com/datalabsindia/batch/TransactionHistoryCsvImport.class] 中定义的名称为“transactionHistoryStep”的 bean 时出错:通过工厂方法实例化 bean失败的; 嵌套异常是 org.springframework.beans。BeanInstantiationException:无法实例化[org.springframework.batch.core.Step]:工厂方法'transactionHistoryStep'抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/datalabsindia/batch/TransactionHistoryCsvImport.class] 中定义名称为“处理器”的 bean 创建错误:通过工厂方法进行的 bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.batch.item.ItemProcessor]:工厂方法“处理器”抛出异常;嵌套异常是 org.springframework.beans.factory.support.ConstructorResolver 的 org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) 的 java.lang.NullPointerException。beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ...省略了33个常见框架原因:org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.batch.core.Step]:工厂方法“transactionHistoryStep”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/datalabsindia/batch/TransactionHistoryCsvImport.class] 中定义名称为“处理器”的 bean 创建错误:通过工厂方法进行的 bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.batch.item.ItemProcessor]:工厂方法“处理器”抛出异常;嵌套异常是 org 的 java.lang.NullPointerException。springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ...省略了56个常见框架原因:org .springframework.beans.factory.BeanCreationException:在类路径资源 [com/datalabsindia/batch/TransactionHistoryCsvImport.class] 中定义名称为“处理器”的 bean 创建错误:通过工厂方法实例化 bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.batch.item.ItemProcessor]:工厂方法“处理器”抛出异常;嵌套异常是 org.springframework.beans.factory.support.ConstructorResolver 中的 java.lang.NullPointerException。invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ... 省略了 57 个常见框架原因: org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.batch.item.ItemProcessor]:工厂方法“处理器”抛出异常;嵌套异常是 org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) 处的 java.lang.NullPointerException org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ...省略了 80 个常见帧原因:java.lang.NullPointerException:com.datalabsindia.batch 处为空。

4

1 回答 1

0

您遇到的异常是由于您尝试使用@Autowired 依赖项,然后您才能保证该依赖项已被注入到您的TransactionHistoryCsvItemProcessor对象中。

工厂方法“处理器”抛出异常;嵌套异常是 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) 处的 java.lang.NullPointerException

这些是有问题的行:

List<TransactionHistory>list= transactionHistoryRepository.findAll();
List<String> pLevel7Products = productHierarchyRepository.getPLevel7Ids();
List<String> level5locations=locationHierarchyRepository.getLevel5Ids();

如果您需要对您的TransactionHistoryCsvItemProcessor对象进行一些额外的初始化,最简单的做法是使用TransactionHistoryCsvItemProcessorimplement InitializingBean,然后List在覆盖的方法中初始化您的 s afterPropertiesSet

例如,_

public class TransactionHistoryCsvItemProcessor 
    implements InitializingBean, ItemStream, ItemProcessor<TransactionHistory,TransactionHistory>{

    @Autowired
    private TransactionHistoryRepository transactionHistoryRepository;
    @Autowired
    private ProductHierarchyRepository productHierarchyRepository;
    @Autowired
    private LocationHierarchyRepository locationHierarchyRepository;

    List<TransactionHistory> list;
    List<String> pLevel7Products;
    List<String> level5locations;

    // ...implementation omitted for brevity

    @Override
    public void afterPropertiesSet() throws Exception {
        // typical null checks would also be prudent
        list = transactionHistoryRepository.findAll();
        pLevel7Products = productHierarchyRepository.getPLevel7Ids();
        level5locations = locationHierarchyRepository.getLevel5Ids();
    }

}
于 2020-05-11T19:41:42.947 回答