SpringApplication Class in spring Boot
In this post, We will take a look at SpringApplication class, What its purpose and How to use SpringApplication class in Spring Boot.
Introduction to SpringApplication class
You may have seen this class already. When generating a Spring Boot project from Spring Initializer or from IDE, You could see this class in the main method. So what is its purpose?
The SpringApplication
class provides a convenient way to bootstrap a Spring application from a main()
method. In most cases, you would use the static run method of this class as shown below.
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
Code language: Java (java)
When you run this main method, You will see the following output.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.4)
2021-04-11 01:39:02.722 INFO 8564 --- [ main] MySpringBootApplication: Starting MySpringBootApplicationusing Java 1.8.0_282 on MY-PC with PID 8564 (C:\repo\github\springhow\spring-boot-app\target\classes started by raja-anbazhagan in C:\repo\github\springhow\spring-boot-app)
2021-04-11 01:39:02.727 INFO 8564 --- [ main] MySpringBootApplication: No active profile set, falling back to default profiles: default
2021-04-11 01:39:03.984 INFO 8564 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-04-11 01:39:03.999 INFO 8564 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-04-11 01:39:03.999 INFO 8564 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.44]
Code language: Java (java)
Most importantly, This class comes with some reasonable defaults for the application to start. So If you want to override any starts up setting, then this class probably has something to do with it.
Ways to create SpringApplication
There are three ways in which you could create a SpringApplication
object. They are,
- Using static
SpringApplication.run
method. - By creating the
SpringApplication
object using constructor and configuring it manually. - Using the Fluent builder api using
SpringApplicationBuilder
.
The static run method is the simple one to use. So Let’s discuss more on the other two.
Create SpringApplication using constructor
Like any other java class SpringApplication class can be created using constructor. In this case, You have a constructor that takes zero or more source classes. So you could rewrite the previous snippet as shown here.
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MySpringBootApplication.class);
application.run(args);
}
Code language: Java (java)
With this approach, You could change Custom Banner, Lazy loading and Application Startup monitoring etc. directly.
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MySpringBootApplication.class);
application.setBannerMode(Banner.Mode.OFF);
application.setLazyInitialization(true);
application.setApplicationStartup(new BufferingApplicationStartup(1024));
application.run(args);
}
Code language: Java (java)
If you do not wish to use the source class at the constructor, you could specify the sources at a later stage.
SpringApplication application = new SpringApplication();
application.setSources(Collections.singleton("com.springhow.examples.springbootapp.MySpringBootApplication"));
Code language: Java (java)
Create using Spring Application Builder
This approach uses builder pattern to create the object. And it is probably the best way to create SpringApplications
. For example, Take a look at this snippet.
public static void main(String[] args) {
SpringApplication application = new SpringApplicationBuilder(MySpringBootApplication.class)
.bannerMode(Banner.Mode.OFF)
.lazyInitialization(true)
.applicationStartup(new BufferingApplicationStartup(1024))
.build();
application.run(args);
}
Code language: Java (java)
As you see, You could chain these builder methods till you complete all the configs. Once you are done with the configs, Call the build method and you have a SpringApplication
object. Further, you could call the run method to boot the application.
Summary
To sum it up, We learned three different ways to boot the spring application. If you liked this article, please drop a comment. Also You may like to read the following write-ups.