Skip to content

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

Open
@spring-projects-issues

Description

@spring-projects-issues

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:

  • One Java configuration class to +declare the Job+ and +import all other configurations+:
@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();
    }
}
  • One XML configuration file to declare the +job flow+:
<beans:beans>
    <flow id="main-flow">
        <step id="step1" next="step2"/>
        <step id="step2"/>
    </flow>
</beans:beans>
  • Another Java configuration class to declare +all Step 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions