Introduction To Spring Framework - Quick Tutorial For Beginners

Image Image

In this article, we look to get an introduction to what the Spring framework is, and why it is so widely used.

You will learn

  • What is Spring Framework?
  • What are the important problems that Spring Framework solves?
  • How does Spring Framework make building enterprise applications easier?
  • What are important features of Spring Framework?
  • What are important concepts to understand in Spring Framework?
  • Why Is The Spring Framework Important?
  • What are the best practices in using Spring Framework?
  • What are Spring Projects?
  • What are Spring Modules?

Free Courses - Learn in 10 Steps

Spring Framework

This is the first article in a series of articles on Spring Framework:

What Is Spring?

Here’s the description from the Spring web site:

The Spring framework provides comprehensive infrastructural support to develop Java applications. Spring handles the infrastructure, so you can develop your application.

What does this mean?

Let’s look at some of the important features of Spring framework with examples.

Writing Loosely Coupled Code

When we write application code, we don’t want it to be tightly coupled.

What Is Tight Coupling?

Tightly coupled code could look like the following:

	public class ComplexAlgorithmImpl {
		BubbleSortAlgorithm bubbleSortAlgorithm = new BubbleSortAlgorithm();

ComplexAlgorithmImpl is tightly coupled to the BubbleSortAlgorithm.

If you need to change the sort algorithm from BubbleSortAlgorithm to QuickSortAlgorithm, then the code of ComplexAlgorithmImpl needs to change as well.

What Is Loose Coupling?

Is there a better way to manage change, than what we see over here? turns out there is - use an interface named SortAlgorithm:

	public interface SortAlgorithm {
		public int[] sort(int[] numbers);

	public class BubbleSortAlgorithm implements SortAlgorithm {

	public class QuickSortAlgorithm implements SortAlgorithm {

	public class ComplexAlgorithmImpl {
		private SortAlgorithm sortAlgorithm;

ComplexAlgorithmImpl is no longer tightly coupled to any particular sort algorithm.

Whenever you need to instantiate a ComplexAlgorithmImpl, this is how you proceed:

	ComplexAlgorithmImpl binarySearch = new ComplexAlgorithmImpl(new QuickSortAlgorithm());

The exact sort algorithm to be used can be passed as an input parameter to the constructor.

What does the Spring Framework do?

Where does the Spring framework come into the picture?

Look at the snippet below:

	public class ComplexAlgorithmImpl {
		private SortAlgorithm sortAlgorithm;

	public class QuickSortAlgorithm implements SortAlgorithm {

We defined the components and identified their dependencies by using Spring specific annotations.

We are defining ComplexAlgorithmImpl as a spring bean, and SortAlgorithm as a dependency. We are also providing an implementation of SortAlgorithm - QuickSortAlgorithm as a spring bean.

When Spring Framework looks at this code, it create a QuickSortAlgorithm bean and injects it into ComplexAlgorithmImpl bean. This is called Dependency Injection.

Advantages Of Loose Coupling

If you want to switch to a new SortAlgorithm implementation, then loose coupling makes it very easy to make the change. The code inside ComplexAlgorithmImpl does not need to change at all.

You need to remove the @Component annotation from QuickSortAlgorithm and place it at the definition of the new implementation.

The other thing you are now able to do is write a unit test for ComplexAlgorithmImpl, without worrying about the sort logic.

You can create a stub algorithm that implements SortAlgorithm, and use that in tests for ComplexAlgorithmImpl. The test would now focus on the business logic of the component, not its dependencies.

Another Example Of Loose Coupling

Have a look at the following code:

	public class WelcomeController {
		private WelcomeService service = new WelcomeService();
		public String welcome() {
			return service.retrieveWelcomeMessage();

The WelcomeController is directly creating an instance of the WelcomeService, and so it is tightly coupled to it.

If you were to use the Spring framework to decouple the two, you do something like:

	public class WelcomeService {

	public class Welcome Controller {
		WelcomeService service;

		public String welcome() {
			return service.retrieveWelcomeMessage();

Now, Spring auto-wires in the WelcomeService dependency to the WelcomeController component. When writing a unit test for this component, we can use a WelcomeService stub instead of an actual dependency.

Solving Other Problems Using Spring

The Spring framework can be used to solve a host of other problems in Java applications. Let’s look at a few of them.

Reducing Duplication In Code

Spring provides a whole range of modules such as:

  • SpringMVC
  • SpringJDBC
  • SpringORM
  • SpringAOP
  • SpringJMS
  • SpringTest

All these frameworks make it easy to write your Java code. They bring in a set of abstractions, that reduce boilerplate code.

Improving Integration With Other Frameworks

Spring does not try to provide solutions for problems that have already been solved. It provides excellent integration with frameworks such as

  • Hibernate for ORM
  • iBatis for object mapping
  • JUnit and Mockito for unit testing

What Does The Spring Framework Do?

The Spring framework facilitates the following:

  • Allows the programmers to focus on the business logic. The key feature is dependency management, and it provides excellent interfacing with external frameworks, by adding layers on top of them.
  • Enables testability, as it enables us to write decoupled code.
  • Allows for inversion of control, through dependency injection. A component is no longer tied to specific dependencies. Instead, the dependency is injected in from outside, during instantiation of the component. The Spring framework takes care of the actual injection.

Best Practices With Spring

Use Spring Initializr

Spring Initializr is a Spring project generation tool. Visit the following website -

image info

You only need to provide the Group Id, the Artifact Id, and the framework dependencies of the project. After that, you can generate the project and get started with Spring.

Consider Spring Boot

Spring Boot makes it very easy to use the Spring framework.

Write Unit Tests

Always make sure you write great unit tests, because Spring makes it easy for you.

Challenges With Spring Framework

Spring is difficult for a starting programmer to understand. However, once you understand it, you cannot stop using it!

Understand Spring Modules

Modules in Spring offer specialized functionality which can be used within a Spring application. Popular ones are:

  • SpringMVC
  • SpringJDBC
  • SpringORM
  • SpringAOP
  • SpringJMS
  • SpringTest

Explore Spring Projects

There are a wide variety of projects that have been done, under the umbrella of the Spring Framework. Popular ones include Spring Cloud, and Spring Web Services.

Do check out our video on the same topic: image info


In this article, we had an introductory look at the Spring Framework. We saw that Spring is a Java framework that makes life easy for a programmer. Spring improves the testability of the application code, and also makes it easy to interface it with external frameworks. Spring comes with its own umbrella of projects, and a rich set of modules as well.

Related Posts

Docker Tutorial for Beginners - with Java and Spring Boot

Learn about Docker - What is Docker? Why Is Docker Popular? How to create Docker Image for a Java Spring Boot App?

Devops Tutorial | DevOps with Docker, Kubernetes and Azure DevOps

What is DevOps? How is it different from Agile? What are the popular DevOps Tools? What is the role of Docker, Kubernetes and Azure DevOps in DevOps. Let's get started with a simple usecase.

Deploy Java Spring Boot Applications to AWS, Azure, GCP with Docker and Kubernetes

In this article, we focus our attention on the cloud. How to learn the cloud and deploy Java Spring Boot Applications to AWS, Azure, GCP with Docker and Kubernetes?

Spring Boot Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. This resources will help you learn and gain expertise at Spring Boot.

Microservices with Spring Boot and Java - Part 1 - Getting Started

Let's learn the basics of microservices and microservices architectures. We will also start looking at a basic implementation of a microservice with Spring Boot. We will create a couple of microservices and get them to talk to each other using Eureka Naming Server and Ribbon for Client Side Load Balancing. In part 1 of this series, lets get introduced to the concept of microservices and understand how to create great microservices with Spring Boot and Spring Cloud.

20+ Spring Boot Projects with Code Examples

At in28Minutes, we have created more than 20 projects with code examples on Github. We have 50+ articles explaining these projects. These code examples will you learn and gain expertise at Spring Boot.

REST API Best Practices - With Design Examples from Java and Spring Web Services

Designing Great REST API is important to have great microservices. How do you design your REST API? What are the best practices?

Index - 500+ Videos

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. Here's a list of video tutorials and courses for you

Creating Spring Boot and React Java Full Stack Application with Maven

This guide helps you create a Java full stack application with all the CRUD (Create, Read, Update and Delete) features using React as Frontend framework and Spring Boot as the backend REST API. We use Maven as the build tool.

Creating a SOAP Web Service with Spring Boot Starter Web Services

Let's learn how to create a SOAP Web Service with Spring Boot Starter Web Services. We will take a Contract First approach by definining an XSD and exposing a WSDL from it.


in28Minutes Best Selling Udemy Courses

Image Image Image Image Image Image Image Image Image

450,000 Learners are pursuing our 31 amazing courses and 6 Learning Paths. Start Now!

Join 450,000 Learners and 30+ Amazing Courses

LEARN DEVOPS - 200+ VIDEOS - Master DevOps with Docker, Kubernetes and Azure DevOps

350,000 Learners are learning everyday with our Best Selling Courses : Spring Boot Microservices, Spring, Spring Boot, Web Services, Hibernate, Full Stack React, Full Stack Angular, Python, Spring Interview Guide, Java Interview, Java Functional Programming, AWS, Docker, Kubernetes, PCF, AWS Fargate and Azure

85,000 Subscribers are learning from our Free Videos on YouTube : JSP Servlets, Spring, Spring Boot, Spring MVC, Hibernate, Eclipse, Maven, JUnit, Mockito, Full Stack - React, Full Stack - Angular, Docker, Kubernetes, AWS, AWS Fargate, PCF and Azure

Here are the recommend articles to read next : Spring Interview Questions, Spring Boot Interview Questions, Microservices, Hibernate, Spring Security, REST API with Spring Boot, Full Stack with React, SOAP Web Services, Exception Handling, Embedded Servers, Spring Data Rest, Spring vs Spring MVC vs Spring Boot, Building Web Application and Spring Data JPA.

You can checkout all our 100+ articles here - All Articles.

Do not know where to start your learning journey? Check out our amazing learning paths:
Learning Path 01 - Spring and Spring Boot Web Applications and API Developer,
Learning Path 02 - Full Stack Developer with Spring Boot, React & Angular,
Learning Path 03 - Cloud Microservices Developer with Docker and Kubernetes,
Learning Path 04 - Learn Cloud with Spring Boot, AWS, Azure and PCF and
Learning Path 05 - Learn AWS with Microservices, Docker and Kubernetes