The title of this blog is inspired from the famous sitcom “How I met your Mother”. Well, this is a short story of How I met Spring Boot.
We are a team of 5 developers among whom there is a so-called “Software Architect”. In this part of the world that I live, the 2 words, “software architect” really mean different things to different people. More on that later.
There was a new requirement to rewrite a system currently maintained using mainframe to a more modern AngularJS/RESTful based application.The back-end would have REST based services which would communicate with an Oracle database using JPA . On the server side, the first task was really to set up the basic back end application and then the developers on the team would start with their use cases. Our architect decided to take up the responsibility of setting up the basic back end and mentioned that it was a tough task. By basic I mean, just the bare bones with all the basic dependencies, configuration against an in memory database to begin with, simple rest service to help the team get up and running.
When the scrum master asked him an estimate , he said he would take around 2 weeks to set up the basic backend code.
Well during the daily stand up meeting, the architect would say that he is still setting up the project and that went on for 2 weeks.At the end of 2 weeks when he checked in the code, what I saw was really shocking. What I saw was strange dependencies, outdated versions of jars, unused code,dependencies. I realized later that he had copy pasted the basic setup from another project.
Well, I said to myself, “Is setting up a project that hard ? I do remember shying away from trying a few things on my own assuming it would be complicated to bring in all the right dependencies, set up the right Spring configuration.I remember I wanted to integrate Spring with Hibernate and never really understood spring-orm library and everything else that came along with it. I have sometimes wondered how to get all configuration right and well sometimes been a little lazy.
But this time, I decided to investigate this further and decided to set up the project and of course nobody in the team knew this. This is when I discovered Spring Boot.
Well, as I was browsing through the official Spring framework website, I read something about bootstrapping your Spring Boot application and this is when I stumbled upon the Spring Initializr. I switched to the full version there and chose the following:
- Maven project
- Java 1.8
- Left the group and artifact id as it is as I was playing around.
- JPA support
- H2 as in memory database and finally
- Full stack web development with Tomcat and Spring MVC
And well on clicking the Generate Project button, I had a full-fledged maven project. That took a few days…Correction – seconds!
I then wrote the following simple classes, one end point for the rest service, one for the repository layer, one JPA entity and finally the class which starts everything.
Snapshot of the pom.xml
Then I did the following:
- Ran mvn clean install
- java -jar ./target\demo-0.0.1-SNAPSHOT.jar ( Yes, that is right, a jar file)
That’s it, the application was up and running ! When I hit the URL :
I got the following output:
Note : The DemoApplication implements a CommandLineRunner and hence the run method. This is being done only to populate some mock data into the H2 database which the rest service can access .
The entire thing took me only a few minutes. We were able to set up and run a Spring REST, JPA based maven project in a matter of few minutes ! Not just that , we were able to get an executable jar file which ran in an embedded server. Now, how cool is that !
Well, I wonder what the architect did during those 2 weeks what we could do in a few minutes! Not just that, we were able to configure the project with all the latest and relevant dependencies without worrying about compatibility and version related issues.
We did not write a single configuration file, there are no xml’s, there is absolutely no code generation behind the scenes !
But what really happened ?
This is all handled by Spring Boot ! The starter pom that we added brings in the required jars to the classpath. As an example ,with the spring-boot-starter-parent and spring-boot-starter-data-jpa dependency , Sprint Boot will bring all the latest versions of the jars(Spring data, Hibernate). In our case it pulls in version 5.0.9 of Hibernate.
Then through the usage of the @SpringBootApplication annotation (see DemoApplication.java) Spring Boot configures some defaults.
Some of the defaults in this case:
- Spring Boot configures an embedded Tomcat server if we haven’t configured it to run on anything else. This is done since we added the spring-boot-starter-web in then pom.xml
- As we did not specify any other database specific parameters, Sprint boot decided to use an in memory H2 database which we added in the pom.xml
- Spring boot configured JPA with Hibernate for us as we added the spring-boot-starter-data-jpa dependency
Of course we can add other specific configurations that we want and in that case Spring Boot will honor that and move out of the way or if we wanted to bring an older version of Hibernate, we just need to modify the pom.xml and add the following:
We were able to run the application using the java -jar command. This is enabled through the spring-boot-maven-plugin dependency we added in the pom.xml.
We have only scratched the surface of how simple it is get a project up and running with a rest based service along with JPA but Spring Boot can do a lot more for us. Not only did we manage to get it up and running quickly but with all the right dependencies and the setup looks clean !
We can integrate spring boot with a nosql database, jms , apache camel, jersey, cloud foundry, service discovery and even use it to monitor the application in production. Take a look at Spring Initializr for all the cool things it can do.
This article was only meant to introduce you to Spring Boot. Well, Spring Boot is just perfect for a lazy developer like me.
Well Barney Stinson from the sitcom “How I met your mother” always wore a suit and one of his famous quote is “Suit Up”. Having met Spring Boot, I must say that we must all Boot up…Spring Boot Up !