JobParameter& ItemReader

时间:2011-06-22 13:40:18

标签: java spring-batch

我正在尝试访问itemReader中的JobParameters,我在弹簧用户指南中遵循了“作业和步骤属性的后期绑定”,但无法使其正常工作。

这是我的配置: 春季批次2.1.8 Spring-core 3.0.5

 <bean id="ipcFileReader" scope="step" class="org.springframework.batch.item.file.FlatFileItemReader">
    <property name="resource" value="#{jobParameters['filename']}"/>
    <property name="lineMapper" ref="lineMapper"/>
    <property name="comments" value="#"/>
</bean>

我的测试班:

@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,StepScopeTestExecutionListener.class})
@ContextConfiguration
public class IpcFileReaderTest {

@Autowired
private ItemReader<Client> ipcFileReader;

public StepExecution getStepExecution(){
    StepExecution execution = MetaDataInstanceFactory.createStepExecution();
    execution.getExecutionContext().putString("filename",System.getProperty("user.dir") + "/src/test/resources/OMC_File/OMC_Test_1.csv");
    return execution;

@Test
public void testReader() throws UnexpectedInputException, ParseException, NonTransientResourceException, Exception{
    Assert.assertNotNull(ipcFileReader.read());
}
}

但我总是遇到这个错误:

Error creating bean with name  'lazyBindingProxy.lazyBindingProxy.ipcFileReader#execution#1234' defined in class path resource [applicationContext-Ipc.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot bind to placeholder: jobParameters['filename']

任何想法?

THX

2 个答案:

答案 0 :(得分:0)

实际上,这是由于一个非常小的错字。

#{jobParameters['filename']}

应该是:

#{jobParameters[filename]}

不需要单引号。

答案 1 :(得分:0)

我正在使用此UnitTest传递JobParameters:

package com.test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "/data-source-context.xml",
        "/env-test-context.xml",
        "/hibernate-context.xml",
        "/job-context.xml",
        "/job-runner-test-context.xml"
        })
public class CopyOfStep1Tests {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    private SimpleJdbcTemplate simpleJdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }

    @Before
    public void onSetUp() throws Exception {
        // delete previous batch
        simpleJdbcTemplate.update("delete from LEGACY_FILE");
    }

    @After
    public void tearDown() throws Exception {
        simpleJdbcTemplate.update("delete from LEGACY_FILE");
    }

    @Test
    public void testStep1() throws Exception {

        Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Date date = sdf.parse("2011/10/20");
        JobParameter jobParameter = new JobParameter(date);
        parameters.put("input.date", jobParameter);
        parameters.put("timestamp", new JobParameter(new Date().getTime()));
        JobParameters jobParameters = new JobParameters(parameters);

        JobExecution jobExecution = jobLauncherTestUtils.launchStep("step1", jobParameters);

        BatchStatus status = jobExecution.getStatus();
        Assert.assertEquals("COMPLETED", status.name());
    }



}