1

Excuse me but this may be a noob question for some but it just crossed my mind and I think it is worth to fix my ideas and get a relevant explanation from some experts.

I just started spring batch Tutorial and i have confusion on haw these application are started. let's take this example on official site

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

and the here is the configuration class

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public DataSource dataSource;


    // tag::jobstep[]
    @Bean
    public Job importUserJob(JobCompletionNotificationListener listener) {
        return jobBuilderFactory.get("importUserJob")
                .incrementer(new RunIdIncrementer())
                .listener(listener)
                .flow(step1())
                .end()
                .build();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .<Person, Person> chunk(10)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    }
    // end::jobstep[]
}

Here it was mentioned that the main() method uses Spring Boot’s SpringApplication.run() method to launch an application.

it' s not clear for me haw the job importUserJob was executed whereas there is no explicit code found that show haw we start this job, it's only a configuration part (declaration).

On the other hand i found another example haw to start a spring application like this:

public static void main(String[] args) {
    GenericApplicationContext context = new AnnotationConfigApplicationContext(MyBatchConfiguration.class);
    JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
    Job job = (Job) context.getBean("myJobName");//this is bean name of your job 
    JobExecution execution = jobLauncher.run(job, jobParameters);
}

Here i can understand that the job is executed with the jobLauncher.

Feres.o
  • 283
  • 1
  • 4
  • 16

1 Answers1

1

This is the Inversion of Control (IOC, or Dependency Injection) in Spring in action. Essentially this is what happens:

  1. The @SpringBootApplication on Application tells Spring that it should search the package of Application and all sub-packages for classes annotated with relevant annotations
  2. The @Configuration annotation on BatchConfiguration is such a relevant annotation and tells Spring that this class contains information about how the application should be configured. This means that Spring will create an instance of this class and read all attributes and methods on it in search of more information.
  3. The @Autowired annotations on the various fields in BatchConfiguration tells Spring that when instantiating this class these fields should be set to the applicable beans (injected).
  4. The @Bean annotation on importUserJob tells Spring that this method produces a bean, this means that Spring will call this method and store the resulting bean under the Job type (and any interfaces/superclasses it inherits) and under the importUserJob name.
  5. Further annotations on the built job will trigger other actions from Spring. It likely contains annotations that tell Spring to run various functions, connect it to some events. Or another bean requests all instances of Job and do stuff with it in the methods it tells Spring to execute.

The @EnableBatchProcessing annotation is a meta-annotation that tells Spring to search other packages and import other configuration classes to make the beans requested for injection (annotated with @Autowired) available.

IOC can be a pretty advanced concept to grasp in the beginning and Spring definitely isn't the easiest IOC-framework there - though it is my personal favourite - but if you want to know more about Spring Framework and IOC I suggest you start by reading the documentation and then continue on by searching for tutorials.

Raniz
  • 10,882
  • 1
  • 32
  • 64
  • Thank you @Raniz, thanks to this description and the comment of Luca i have been able to clarify the entire process – Feres.o Nov 09 '17 at 14:52