Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Job/Step/Reader/Writer all be bean?

Tags:

spring-batch

As fars as all the examples from the Spring Batch reference doc , I see that those objects like job/step/reader/writer are all marked as @bean, like the following:

@Bean
public Job footballJob() {
    return this.jobBuilderFactory.get("footballJob")
                     .listener(sampleListener())
                     ...
                     .build();
}

@Bean
public Step sampleStep(PlatformTransactionManager transactionManager) {
    return this.stepBuilderFactory.get("sampleStep")
                .transactionManager(transactionManager)
                .<String, String>chunk(10)
                .reader(itemReader())
                .writer(itemWriter())
                .build();
}

I have a scenario that the server side will receive requests and run job concurrently(different job names or same job name with different jobparameters). The usage is to new a job object(including steps/reader/writers) in concurrent threads, so I propabaly will not state the job method as @bean and new a job each time.

And there is actually a differenence on how to transmit parameters to object like reader. If using @bean , parameters must be put in e.g. JobParameters to be late binding into object using @StepScope, like the following example:

@StepScope
@Bean
public FlatFileItemReader flatFileItemReader(@Value(
"#{jobParameters['input.file.name']}") String name) {
return new FlatFileItemReaderBuilder<Foo>()
.name("flatFileItemReader")
.resource(new FileSystemResource(name))
}

If not using @bean , I can just transmit parameter directly with no need to put data into JobParameter,like the following

public FlatFileItemReader flatFileItemReader(String name) {
return new FlatFileItemReaderBuilder<Foo>()
.name("flatFileItemReader")
.resource(new FileSystemResource(name))
}

Simple test shows that no @bean works. But I want to confirm formally:

1、 Is using @bean at job/step/reader/writer mandatory or not ?

2、 if it is not mandatory, when I new a object like reader, do I need to call afterPropertiesSet() manually?

Thanks!

like image 473
Leo Avatar asked Oct 17 '25 02:10

Leo


1 Answers

1、 Is using @bean at job/step/reader/writer mandatory or not ?

No, it is not mandatory to declare batch artefacts as beans. But you would want to at least declare the Job as a bean to benefit from Spring's dependency injection (like injecting the job repository reference into the job, etc) and be able to do something like:

ApplicationContext context = new AnnotationConfigApplicationContext(MyJobConfig.class);
Job job = context.getBean(Job.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
jobLauncher.run(job, new JobParameters());

2、 if it is not mandatory, when I new a object like reader, do I need to call afterPropertiesSet() manually?

I guess that by "when I new a object like reader" you mean create a new instance manually. In this case yes, if the object is not managed by Spring, you need to call that method yourself. If the object is declared as a bean, Spring will call the afterPropertiesSet() method automatically. Here is a quick sample:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TestAfterPropertiesSet {

    @Bean
    public MyBean myBean() {
        return new MyBean();
    }

    public static void main(String[] args) throws Exception {
        ApplicationContext context = new AnnotationConfigApplicationContext(TestAfterPropertiesSet.class);
        MyBean myBean = context.getBean(MyBean.class);
        myBean.sayHello();
    }

    static class MyBean implements InitializingBean {
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("MyBean.afterPropertiesSet");
        }

        public void sayHello() {
            System.out.println("Hello");
        }
    }

}

This prints:

MyBean.afterPropertiesSet
Hello
like image 137
Mahmoud Ben Hassine Avatar answered Oct 19 '25 13:10

Mahmoud Ben Hassine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!