Maria
Maria

Reputation: 604

Execution process in Spring boot

New to spring-boot, checked online examples. Got one doubt.

Main calling class

@SpringBootApplication
public class Application {

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

Batch Processing class

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

  --
  --
  so on

}

SpringApplication.run(Application.class, args) creates an appropriate ApplicationContext instance and load beans.

I was trying to figure out that, what's a flow of code (step wise execution). Then after setting up logger at different places, figure out that BatchConfiguration class methods are getting executed in sequence, and it covers the execution of whole code too.

Checked this class, its headed with these two tags @Configuration, @EnableBatchProcessing. Is it correct understanding that, once SpringApplication.run is completed, spring-boot looks for class having @Configuration, @EnableBatchProcessing tags on it and process everything from there ? Is it a driver class ?

If above understanding is correct, how many of classes of this type (BatchConfiguration) we can have in one spring-boot application ? If more than one, than what would be the flow then ?

Upvotes: 0

Views: 1852

Answers (2)

Poojitha_naidu
Poojitha_naidu

Reputation: 26

  • After creation of Spring starter project, we get a main class which contains @SpringBootApplication, under that class we have SpringApplication.run(Application.class, args). This is called Run method.
  • When we go inside the run method, It internally creates the object of Spring application and it calls internally Run method.
  • If we use debug pointer there, we can see the logic. This logic will execute the spring application whenever we run.
  • In the run method, we have Stopwatch to identify the time taken by our spring boot application. After starting the Stopwatch, it will call getRunListeners(args) to listen the listeners in main method.
  • After that we have a prepared environment by listeners. And it will print the Spring banner.
  • After that it creates ApplicationContext.(Run method internally creates ApplicationContext).
  • Then Run method refreshes the context and stops the Stopwatch. Then it will internally call the Runners. There are two types of runners, ApplicationRunner and CommandLineRunner. It will trigger and returns the context.

Upvotes: 1

GeertPt
GeertPt

Reputation: 17854

A couple of points.

  • SpringApplication.run(Application.class, args) will scan all classes in the same package or below of Application.class, initialize all beans in the applicationContext and dispatch application events.
  • Classes with @Configuration on are meant to define beans. Beans will be reordered to allow dependencies between beans, and each bean will be initialized using its @Bean-annotated method.
  • The @EnableBatchProcessing only needs to be on one @Configuration class. It will auto-configure more beans that you can use as dependencies, like the jobBuilderFactory and stepBuilderFactory, and a jobLauncher. The jobLauncher will have dependencies on all beans of type 'Job', so it will be initialized last.
  • Each bean can listen for applicationEvents by implementing a certain interface, and define a response for that. The jobLauncher will listen for the 'application started' event and respond by executing all its jobs, either on the main thread, or using a thread pool if that was defined.

See BatchAutoConfiguration on how you can configure the behaviour of the auto-configured beans.

Upvotes: 1

Related Questions