2. At the time of SpringApplication object creation

 There are two overloaded constructors in SpringApplication class.


 

This is dummy constructor which uses the other constructor. This constructor is used when we do not have the resource loader.


 

  • On line no 281 your provided class the main class is registered. 
  • On line no 282 it checks what kind of application it is. By the time I am writing this blog Spring introduced three kind of web application type. NONE, SERVLET and REACTIVE. But wait how does it decide that its what kind of application?
    • With the help of ClassLoader it checks the classpath whether only servlet of  reactive type is present i.e. org.springframework.web.reactive.DispatcherHandler. If only this servlet is present then it is confirmed that it is a  REACTIVE kind of web application.
    • For normal web application it checks for the only servlet class i.e. javax.servlet.Servlet, org.springframework.web.context.ConfigurableWebApplicationContext.

  • On line no 283 first all the instances of the Bootstrapper.class is created and added to the ArrayList. I know you have many questions here! but wait I will tell you each and everything in detail.
    • First of all what do we mean by all the instances of the Bootstrapper.class
    • Spring will create all the objects of Bootstrapper type.
    • Now you might think how does spring do that?
    • There is a folder named META-INF present in multiple jars of your project library. Inside this folder there is file named spring.factories, all classes which is present inside this file as a string key and  comma separated values are instantiated here and stored. To load the classes spring uses ClassLoader and to instantiate the object it uses ClassUtils.
  • On line no 284 and 285 same thing happens, all objects of type ApplicationContextInitializer and ApplicationListener are created respectively using the class name present in file spring.factories.
  • On line no 286 they get the stacktrace via throwinig the RuntimeException and get the name of the main class from where the SpringApplication run method is called. This is achieved by matching the main method name. I know few folks know this but I am a learner and I think this technique is useful and we can use it in production.

So, guys as of now only these things happens when the object of SpringApplication class is created. But guys this is just a debug stuff right? Where is the analysis part of it?

Here comes the analysis:

  • To call the run method of the SpringApplication class it is mandatory to pass the  main class type object.
  • Now we know what dependency is required for reactive and non-reactive web application. If you are not including the reactive and web dependency then normal desktop application is created.
  • If you know how to debug then you will have three answers:
    • What Bootstrappers are loaded?  How and when we can use it?
    • What ApplicationContextInitializers are loaded? Where to use and how to use?
    • What ApplicationListeners are loaded by default and how to use it for the application?

 

 Wait for my next blog till then, Jai Hind!

Comments

Popular posts from this blog

1. What happens after the start of main class?