X
    Categories: Spring

Introduction to Spring Web Framework

In this article I will explain the key concepts behind Spring Framework and how to use it further to build web apps.

What is Spring Framework?

Spring Framework is an open-source application container for Java that supplies many useful features, such as Inversion of Control, Dependency Injection, abstract data access, transaction management, and more. Spring was initially introduced back in 2002 by Rod Johnson as a 30K lines of code framework accompanying his book “Expert One-on-One J2EE Design and Development”. The framework was initially known as “interface21” but was later renamed and released as an open source project under the name “Spring”. You can read the full story at Spring’s blog.

Rod Johnson – creator of Spring



Spring first major release came in 2004 and it was a game-changer for Java EE applications. At this time the J2EE specification was lacking functionality and was difficult to use. Spring was accepted as alternative to J2EE (or even a replcement) and still is. Even nowadays it has numerous of features not present in Java Enterprise Edition Specification.

Spring versions and time of release:

  • version 2.0 in 2006
  • version 2.5 in 2007
  • version 3.0 in 2009
  • version 3.1 in 2011
  • version 3.2 in 2012
  • version 4.0 in 2013
  • version 4.2 in 2015
  • version 4.3 in 2016
  • version 5.0 in 2017, which is the most current version

Authors note: Although this is a Spring tutorial I am not advocating in favor of Spring compared to Java EE. On this page (Java Tutorial Network) you will find an entire section of Java EE tutorials too. Both technologies have pros and cons. Spring provides many features and can even run outside the enterprise container, such as Application servers etc. Java EE on the other hand has grown to a mature and solid specification with reliable implementations. It’s up to you to decide if you want to implement pure Java EE, Spring or a mixture of both. Bigger companies still prefer to use Java EE where middle-sized organisations and startups prefer Spring (of course there are exceptions of this “rule”). The comments section is open for dispute on this topic.

What is Inversion of Control (IoC)?

One of Spring’s core features it the usage of the concept Inversion of Control (IoC). IoC is about inverting the flow of control that the traditional programming model used to have in terms of objects at the higher-level handling the creation and management of lower-level objects life cycle. In the IoC programming model, higher-level objects rather receive one or more instances of these dependencies from the calling object or external framework. This is why IOC is also termed Dependency Injection, wherein the dependencies are injected appropriately, and, objects bother themselves solely with the program execution and not with the object creation.

Simply said Inversion of Control (IoC) and Dependency Injection (DI) patterns are all about removing dependencies from your code.

For example, let’s say your application has a map component and you want to provide a parser to translate the map coordinates. Your standard code will look like this:

public class Map {
	private CoordinatesParser parser;
	
	public Map() {
		this.parser = new CoordinatesParser();
	}
}

In the code above we create a dependency between Map and CoordinatesParser, by using this.parser = new CoordinatesParser(); As you see the Map class directly depends on the CoordinatesParser class.

Next example shows how to avoid this by using interface and constructor injection

public class Map {
	private ICoordinatesParser parser;
	
	public Map(ICoordinatesParser parser) {
		this.parser = parser;
	}
}

In the example above instead of using a class we use an interface ICoordinatesParser. This way we are creating an abstraction by having the CoordinatesParser dependency class in Map constructor signature (not initializing dependency in class). This allows us to call the dependency then pass it to the Map class like so:

ICoordinatesParser parser = new CoordinatesParser(); // dependency
Map map = new Map(parser);

This is just a simple example. IoC is too general to be described with one strict example. There are multiple scenarios you can implement DI by using setter injection, constructor injection or interface injection.

What’s next?

In the next tutorials I will demonstrate how to use Spring framework to build web applications. You will learn how to build RESTful web services, connect to a database and secure your applications.

5 3 votes
Article Rating
filip:

View Comments (1)

  • Nice website and collection of tutorials. Yet to explore in detail. I liked this article as you had clearly said that you are not promoting any framework (Java EE Vs Spring) and the website has a good number of tutorials on Java EE as well. Very rare websites I have seen and I appreciate the thought process.

    Way to go..

Related Post