Friday, July 31, 2015

Introduction to Spring Boot as a Servlet Based Web Application Framework

This is the fifth and last post in the series on Introduction to servlet based web application in java. The previous posts in this series are:
  1. Introduction to the Servlet API, Web Server and Servlet Container
  2. Introduction to .war Packaging and Maven
  3. Quick Overview of Basic Maven Concepts
  4. How to Create .war Files with Maven Archetypes and Deploy to Tomcat
  5. How to Create .war Files with IntelliJ and Deploy to Tomcat
These posts have touched on web servers and containers, the servlet API, packaging war files and how to achieve this using built in support in IDE or via maven archetype. This post would conclude the series by taking a look at Spring boot.


Why A Framework?

It is possible to build a web application by directly programming against the servlet API, but as it always goes with Software development, frameworks usually pop up that seek to remove the tedious repetitive infrastructure related tasks, so focus can be on the business logic side of things: Spring boot is such a framework.

This post will provide some background information, explain some basic concepts regarding Spring boot and how it relates to Spring framework. Like other posts in this series it is geared towards the reader who is approaching servlet based web application development in Java for the first time. The information provided in this post should help such a reader see how Spring fits into the whole setup.

Instructions on how to set up a project etc would be differed to other resources online. There are tons of useful and helpful guides out there, especially on the official reference guide; so this post will only spend some time on the why and what of the framework.

What is Inversion of control
When hungry, you could either cook yourself a meal...you know, be responsible for sourcing and getting all the ingredients and making sure they are put together and cooked...or...

You could either go to a restaurant, where you specify what kind of meal you want, how you want it, and in what quantity and the chefs will make sure you get what you want...Inversion of control is somewhat similar to this.

With Inversion of control you hand over the control of how your system would be put together in the hands of another system; you only need to write the components of your system and then provide the instructions on how they should be assembled so as to work together.

Yes it is more elaborate than this, but this metaphor should help get the general gist. For a more detailed explanation of what Inversion of control is, do check Inversion of Control Containers and the Dependency Injection pattern, a write up by Martin Fawler.

What is Spring framework and Spring MVC
Spring is a framework that allows the building of applications following the Inversion of control idea.

It is often referred to as an Inversion of control container, which is quite apt, as most of the other aspect of Spring is built around the Inversion of control concept. For example the Spring MVC sub project, which supports building web application using the MVC pattern is also implemented around the core Inversion of control of Spring framework.

For more information, read the Overview of Spring Framework

What is Spring boot
Spring boot is a project that builds on Spring Framework with the sole aim of making the process of creating a Spring framework based application easier.

Historically, setting up a Spring framework application could be very time consuming with a lot of manually configuration required. And over the years, there has been efforts to make the setup process less laborious. Spring boot seems to be the ultimate answer to easing the bootstrapping step for Spring powered web applications.

Spring boot achieves it’s aims of simplicity via 2 main features:

1. Starter Poms: Which is a packaged pom that bundles together dependencies that are known to work together in other to achieve certain functionality. For example, to enable Spring MVC capability, you will need to state about 3 different dependency, but with Spring boot, all that is required is specifying spring-boot-starter-web as a dependency and all the dependency required to have Spring MVC will be made available.
2. Autoconfiguration based on classpath's content: Which is Spring boot automatically wiring up it’s components based on the classes it finds in your classpath.

Spring boot is opinionated and favours convention over configuration, but it is made in such a way that it gets out of the way in case you want to take control of the setup and wiring process. Which is a very cool thing.

Getting Started With Spring Boot
Getting started with Spring boot is so ridiculously easy, that what needs to be done can be written in two bullet points:

1. Go to https://start.spring.io/ and specify the details of your project. Click "generate project"
2. Import the generated project into your IDE.

And you are good to go.

The SPRING INITIALIZR helps in bootstrapping a Spring boot application, sort of similar to what we can do with Maven Archetypes. It allows us to specify a project's metadata and its dependencies and create project based off these information.

It is possible to also access the initializer service via a cURL client or via integration in your IDE

That is mostly how to bootstrap a Spring boot application. For more coverage of the framework and how it can be put to use, please consult the various links listed in the Additional resources section below.

Although this post touched on Spring boot as an example of a framework for Servlet based web application in Java, it is worth mentioning that other great frameworks do exists out there. Some of the modern ones includes Ratpack, Spark, or Dropwizard: frameworks which I would advice a curious reader to also check out.

Additional Resources


No comments: