Spring Boot startup for an application is an interesting topic to explore. It will help you understand how a Spring Boot Application downloaded from the Spring Boot Initializr actually works.
Understanding the basic Spring Boot startup process will also help us develop Spring Boot Microservices.
In the last post in this series, we cooked up a Spring Boot Application using https://start.spring.io.
Then, we used IntelliJ to run our application and finally tested it by hitting the actuator end-point. In this post, we will dive deeper and try to understand the process behind Spring Boot startup.
The Dependencies
A Spring Boot project will usually have several dependencies. If you’ve used maven, you can see those dependencies in the pom.xml file of your project.
To start off, the most important part is the spring-boot-starter-parent. This is a special starter provided by Spring Boot. It’s job is to provide useful Maven defaults and a dependency management section. As a result, it allows us to omit version numbers in other dependencies.
1 2 3 4 5 6 | <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> |
Next are the actual dependencies as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> |
Notice that we don’t put version tags in these dependencies.
A quick walk-through of these dependencies is like this:
Spring Boot Starter Web – This helps enable your Spring Boot application as a web application. It also brings in Tomcat that will be used to serve your application.
Spring Boot Starter Data JPA – This brings support for JPA and Hibernate as well as configures the data sources available on the classpath.
H2 – H2 is an in-memory database. It is very useful for quickly iterating through changes during development phase. When Spring sees H2 on the classpath, it will configure a data source for us. It will also wire up the datasource in our application context.
Actuator – Spring Boot actuator exposes certain endpoints from your application. These endpoints allows us to ask questions about the health of our application and also query for other run time statistics. This feature comes in very handy when we have to implement automated health-checks for our application.
Spring Boot Startup – The Main Class
Every Spring Boot application has a main class. This is the class that deals with the Spring Boot startup process.
The main class of our application looks as below.:
1 2 3 4 5 6 7 8 | @SpringBootApplication public class SpringBootStarterApplication { public static void main(String[] args) { SpringApplication.run(SpringBootStarterApplication.class, args); } } |
Notice the @SpringBootApplication annotation. Behind the scenes, Spring does a lot of heavy-lifting just because of that one annotation.
The @SpringBootApplication is equivalent to a combination of the below annotations:
@Component – This tags the class as a source of bean definitions that you might want to wire up into the application context.
@EnableAutoConfiguration – This annotation instructs Spring Boot to start adding other beans to the context depending on the property or classpath settings. For example, if spring-webmvc is on the classpath, this annotation will activate key behavior such as setting up a DispatcherServlet.
@ComponentScan – This instructs Spring to look for other components, configuration classes and services in the same package heirarchy. As a result of this, you can declare beans in your classes that will be wired to the application context during startup.
Finally, there is the main() method. This method, in turn, calls the run() method of SpringApplication class to launch the application.
And that’s about it. No XML Configuration required. No web.xml file.
As you can see, Spring Boot allows you to write Spring Applications using pure Java configuration. And even with that, you don’t have to write any infrastructure setup logic. Everything is taken care of by Spring Boot.
In the next post, we will start off by setting up H2 database for our Spring Boot application.
2 Comments
Juan · March 15, 2020 at 1:07 am
I think it is @Configuration instead of @Component in “@Component – This tags the class as a source of bean definitions that you might want to wire up into the application context.”
Saurabh Dashora · April 13, 2020 at 5:03 am
Hi Juan,
@Configuration also does more or less the same thing.