C.J. De Leon
4 min readMar 1, 2021

--

What is Spring Framework?

Spring is an open-source application framework. It provides infrastructure support for developing Java applications. It is a software library that provides a boatload of predefined code for solving common problems in specific domains. Spring is known to be the world’s most popular framework because it makes Java programming easier and promotes good programming practices by relying on a POJO-based programming model. It can be used for multiple domains, including data access, authentication, batch applications, and many more.

Why use Spring?

Java programs are complex and have many heavyweight components. These components heavily rely on the OS for their appearance and properties. Spring is designed to reduce the amount of resources needed for Java applications while being both secure and flexible. Spring allows programmers to code efficiently and reduce overall application development time because it is lightweight, efficient at managing system resources, and provides a lot of functionality.

How does Spring even work?

When developing a web application, using three-layered architecture, each layer is dependent on the other for an application to work. The presentation layer sends messages to the business logic layer and the business logic layer sends messages to the data access layer. An application can have up to thousands of classes and many dependencies that need to be managed.

Without using Spring Framework or a framework with similar functionality, application code ends up becoming tightly-coupled or interdependent, which is not an ideal coding practice. Instead, loose-coupling is preferred because components become independent, and changing one component will not heavily affect the other, allowing for more coding flexibility. Spring tackles this problem and introduces loose-coupling by the use of Dependency Injection.

To explain Dependency injection:

Dependency Injection is a technique whereby one object (or static method) supplies the dependencies of another object.

A dependency is when one class is reliant on another.

For example, class A uses functionality from class B. It can then be said that class A is dependency of class B. In Java, in order to use methods from another class, we first need to create an instance of that class.

Ex:

public class ObjectA{

private ObjectB object;

ObjectB.doSomething();

}

But how is it different from how dependencies are normally made?

Let’s say that we have an Exhibit class, and an Exhibit class contains objects like Monkey, Gorilla and Trainer. Without DI, the Exhibit class becomes responsible for creating all the dependency objects. If we needed to change the Monkey and Gorilla objects to Mammoth and SabreToothTiger, we will need to recreate the Exhibit object with new Mammoth and SabreToothTiger dependencies. However, with Dependency Injection, we are able to inject the dependencies into the Java application during runtime.

How does Spring allow Dependency Injection?

Spring makes use of the concept of Inversion of Control which is in short, a process in which an object defines its dependencies without creating them. An object is first defined as a Spring Bean. When an object becomes a Spring Bean, it allows the Spring IoC (Inversion of Control) container to manage it, and dictate how it is instantiated or assembled.

This is done by adding the @Component annotation onto a java class:

@Component

public class ComponentClass {

private String name;

public ComponentClass() {

this.name = “Name”;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

This will allow the Spring IoC container to create an instance of the class within the container upon startup of the application.

We can use the following code in addition to the ComponentClass to demonstrate how this works:

@SpringBootApplication

public class DemoApplication {

public static void main(String[] args) {

ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);

ComponentClass A = context.getBean(ComponentClass.class);

System.out.println(A.getName());

}

}

Result:

Name

In this case, the ConfigurableApplicationContext object will allow us to retrieve a reference to the ComponentClass class instantiated on startup using its .getBean(class) method. The application prints the Name value assigned to the ComponentClass in its default constructor.

Retrieving the ComponentClass Spring Bean again will only return the reference to the same object that is instantiated, as shown using the code below:

@SpringBootApplication

public class DemoApplication {

public static void main(String[] args) {

ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);

ComponentClass A = context.getBean(ComponentClass.class);

ComponentClass B = context.getBean(ComponentClass.class);

B.setName(“Different Name”);

System.out.println(A.getName());

System.out.println(B.getName());

}

}

Result:

Different Name

Different Name

But how do we use this to inject dependencies?

Ex.
@Component

public class Service{

public String doThis(){

return “Other Name”;

}

}

Because the Service class has the @Component annotation, it will be instantiated and loaded into the Spring container as a Spring bean.

@Component

public class ComponentClass {

@Autowired

Service service;

private String name;

public ComponentClass() {

this.name = service.doThis();

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

Normally, running service.doThis() like the code above shows will throw a NullPointerException because the Service class has not be instantiated yet. However, using the @Autowired annotation will automatically assign a reference to the Spring Bean that is already instantiated on startup to the Service object.

Using the 1st DemoApplication code, the results should be as follows:

Other Name

There Is much more to the Spring framework than Dependency Injection. Spring Boot, Spring Web, Spring MVC, Spring Security and many others are part of the reason why it’s become such a widely used Java framework. It’s an application framework that’s sure to be useful in many different fields in the industry.

--

--