spring-projects / spring-batch

Spring Batch is a framework for writing batch applications using Java and Spring
http://projects.spring.io/spring-batch/
Apache License 2.0
2.73k stars 2.35k forks source link

Fail to load Application Context when mixing Java and XML configurations [BATCH-2444] #1158

Open spring-projects-issues opened 9 years ago

spring-projects-issues commented 9 years ago

Géraud opened BATCH-2444 and commented

I tried to mix Java configuration classes and XML configuration files in a Spring Batch project.

In a nutshell: I want to use Java configurations everywhere, except for the job flow declaration (flow declared in an XML file).

Here is the complete Job declaration:

@Configuration
@Import(StepsConfig.class)
@ImportResource({"classpath:org/galak75/mixedconfig/batch/main-flow.xml"})
public class MainConfig {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    @Qualifier("main-flow")
    private Flow jobFlow;

    @Bean
    public Job mixedConfigJob() {
        return jobs.get("mixed-config-job")
                .start(jobFlow)
                .build()
                .build();
    }
}
<beans:beans>
    <flow id="main-flow">
        <step id="step1" next="step2"/>
        <step id="step2"/>
    </flow>
</beans:beans>
@Configuration
public class StepsConfig {

    @Autowired
    private StepBuilderFactory steps;

    @Bean
    public Step step1() {
        return steps.get("step1")
                .tasklet(commonTasklet())
                .build();
    }

    @Bean
    public Step step2() {
        return steps.get("step2")
                .tasklet(commonTasklet())
                .build();
    }

    @Bean
    public Tasklet commonTasklet() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println(String.format("Executing %s ...", chunkContext.getStepContext().getStepName()));
                return null;
            }
        };
    }
}

This configuration leads to a BeanCreationException

BeanCreationException: Error creating bean with name 'step1': Instantiation of bean failed; nested exception is java.lang.IllegalStateException: No bean class specified on bean definition

I created a JUnit test (available on GitHub) to illustrate this bug: https://github.com/galak75/spring-batch-labs/blob/master/src/test/java/org/galak75/mixedconfig/batch/MixConfigJobTest.java


Affects: 3.0.5

Reference URL: https://github.com/galak75/spring-batch-labs/blob/master/src/test/java/org/galak75/mixedconfig/batch/MixConfigJobTest.java

spring-projects-issues commented 9 years ago

Géraud commented

This issue title might not be very accurate... It's more about being able to reference a step bean in XML batch configuration

It looks like in XML config, I'm not able to use a reference to a step defined in another configuration resource; something like a ref attribute on batch:step XML element:

<beans:beans>
    <flow id="main-flow">
        <step ref="step1" next="step2"/>
        <step ref="step2"/>
    </flow>
</beans:beans>