繁体   English   中英

Spring Batch自定义ItemReader IllegalArgumentException

[英]Spring Batch Custom ItemReader IllegalArgumentException

我正在尝试实现Spring Batch自定义ItemReader,并且遇到IllegalArgumentException的问题。

异常跟踪在这里;

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reader' defined in class path resource [com/chrisbeech/batch/JobConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) ~[spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) ~[spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~    [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at com.chrisbeech.batch.Application.main(Application.java:37) [classes/:na]
Caused by: java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
at org.springframework.util.Assert.notNull(Assert.java:115) ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.util.Assert.notNull(Assert.java:126) ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.batch.item.adapter.AbstractMethodInvokingDelegator.afterPropertiesSet(AbstractMethodInvokingDelegator.java:135) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 13 common frames omitted

我还附加了自定义的ItemReader ...

package com.chrisbeech.batch.step;

import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.adapter.ItemReaderAdapter;
import org.springframework.stereotype.Component;
import com.chrisbeech.batch.mongodb.collections.Client;

@Component
public class Reader extends ItemReaderAdapter<Client> {

private static final Logger log = LoggerFactory.getLogger(Reader.class);

private int nextClientIndex;
private List<Client> clients;

public Reader(){
    populate();
}

// populate in memory array
private void populate(){

    clients = new ArrayList<Client>();

    Client beryl = new Client();
    beryl.setFirstName("Beryl");
    beryl.setLastName("A");
    clients.add(beryl);

    Client frank = new Client();
    beryl.setFirstName("Frank");
    beryl.setLastName("A");
    clients.add(frank);

    Client paul = new Client();
    beryl.setFirstName("Paul");
    beryl.setLastName("A");
    clients.add(paul);

    Client judith = new Client();
    beryl.setFirstName("Judith");
    beryl.setLastName("A");
    clients.add(judith);

    Client lizzy = new Client();
    beryl.setFirstName("Lizzy");
    beryl.setLastName("A");
    clients.add(lizzy);

    nextClientIndex = 0;
}

// read the people
public Client read() throws Exception, UnexpectedInputException, ParseException {
    Client nextClient = null;

    if(nextClientIndex < clients.size()){
        nextClient = clients.get(nextClientIndex);
        nextClientIndex++;
        log.info("### Reading in " + nextClientIndex + " : " + nextClient);
    }

    return nextClient;
}   

}

...批量作业配置...

package com.chrisbeech.batch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.data.MongoItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.chrisbeech.batch.config.InfrastructureConfiguration;
import com.chrisbeech.batch.mongodb.collections.Client;
import com.chrisbeech.batch.step.ClientProcessor;
import com.chrisbeech.batch.step.Reader;

@Configuration
@EnableBatchProcessing
public class JobConfig {

private static final Logger log = LoggerFactory.getLogger(JobConfig.class);

@Autowired
private JobBuilderFactory jobBuilderFactory;

@Autowired
private StepBuilderFactory stepBuilderFactory;

@Autowired
public JobLauncher jobLauncher;

@Autowired
public JobRegistry jobRegistry;

@Autowired
public InfrastructureConfiguration infrastructureConfiguration;

// define the reader (which is custom)
@Bean
public ItemReader<Client> reader(){
    return new Reader();
}

// define the processor
@Bean
public ItemProcessor<Client, Client> processor(){
    return new ClientProcessor();
}

// define the writer
@Bean
public ItemWriter<Client> writer() {
        MongoItemWriter<Client> writer = new MongoItemWriter<Client>();
        try {
            writer.setTemplate(infrastructureConfiguration.mongoTemplate());

        } catch (Exception e) {
            log.error(e.toString());
        }
        writer.setCollection("Clients");
    return writer;
}


// create the step
@Bean
public Step step(){
    return stepBuilderFactory.get("step")
            .<Client, Client> chunk(1)
            .reader(reader())
            .processor(processor())
            .writer(writer())
            .build();
}

// create the job
@Bean
public Job springBatchJob(){

    return jobBuilderFactory.get("spring-batch-test")
            .incrementer(new RunIdIncrementer())
            .flow(step())
            .end()
            .build();
}
}

...和POM ...

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.chrisbeech.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<version>0.1.0</version>

<properties>
    <java.version>1.8</java.version>
    <oracle.driver.version>11.2.0</oracle.driver.version>
    <junit.version>3.8.1</junit.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
    <relativePath></relativePath>
</parent>

<dependencies>
    <!-- spring boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>  
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>        
    </dependency>       
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

    <!-- ORACLE database driver -->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>${oracle.driver.version}</version>
    </dependency>

    <!-- JUnit framework -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
</project>

任何帮助将不胜感激!

比克

我通过清理Maven存储库解决了这个问题,这可能是由于各处的Spring库版本不同而导致的类路径肮脏所致。 清理它,并将其与具有正确版本依赖关系的一个和相同的Spring版本对齐

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM