By: Kustian, Analyst at Mitrais

Spring Boot

You might already use Spring-Boot in your project and you are happy with it. But do you really know how Spring-Boot works? Can you explain the differences to Spring?

Some of my colleagues asked me what Spring-Boot is and what are the differences to Spring. This question is also asked by some of them who have already used Spring in their projects.

The answer is that Spring-Boot is just Spring with extra magic!

To understand how the magic works, let me tell you the story of where there are two developers, Jeff and Jim, who are working on the same project with a different approach. Jim is a Senior Developer who has used Spring for years and Jeff is a Junior Developer who has just started using Spring. Both of them were assigned to create a simple application that can write a report to the console. Jeff decided to create the application with Spring while Jim is using Spring-Boot. Let’s take a look at their code.

Jeff’s code:

 

Jim’s code:

As we can see, Jeff's code is a lot simpler that Jim's. But if you take a closer look, Jeff's code explains how Spring-Boot works:

  • The annotations @Configuration and @ComponentScan are not necessary in Spring-Boot because they are already part of @SpringBootApplication annotation.
  • Jim has to configure the application context manually which is automatically configured in Spring-Boot.
  • The interface ApplicationRunner is a bean type to mark a bean that should be executed by Spring. It's similar with interface CommandLineRunner in Spring-Boot.

 

The next week they were assigned to retrieve data from a database for the report. In this case they both used embedded databases. Here are their respective codes.

Jeff’s code:

 

Jim’s code:

Again, Jeff’s code is simpler and only focuses on what he’s going to achieve because Spring-Boot will handle the necessary configuration. Meanwhile Jim has to configure the database configuration first by creating JdbcTemplate bean before working on his main task. If you think that Jim’s code explains what Spring-Boot has done under the hood than you are correct. And it’s not something new, it’s provided by Spring.

Conclusions:

  • Spring-Boot provides the necessary Spring configuration so that we can focus on what we are going to do, but still using original Spring features.
  • Understanding how Spring-Boot works is a great advantage, as this will avoid us writing the same code/feature already provided by Spring-Boot. For advance use cases we can override/extend the Spring-Boot-provided configuration to match our needs. If you are interested in the details you can checkout Spring Boot features here and tons of Spring Boot auto-configure here.

Full source code available on Github