All you need to know about Spring and its Architecture

SitaRamSai
4 min readMay 8, 2021

--

Spring is an open-source Java framework that is used by almost every company even today because of its principles that it is based on. They are Inversion of Control and Dependency Injection. Let’s see what they are in detail.

With the help of Spring, we can write loosely coupled code that is not dependent on other classes. This makes code much simpler to write and also spring takes care of providing the dependencies at the run time. That is how spring is inverting the control by taking responsibility for providing the dependency.

This is generally referred to as Inversion of Control (IoC).

Let’s understand that with an example:

Basic terminology required in order to proceed further:

Beans: Beans are the spring objects that are created and managed by Spring. As we know that spring manages all the dependencies of an object, and it instantiates the object after injecting the required dependency.

The common method to define a bean is to add a “@Component” annotation.

Autowiring: Autowiring is the process of finding the dependency and providing the respective class/method.

This is basically done by adding a “@Autowire” annotation.

Now that you understood these two terminologies and their working we can move forward.

In the below example, we can see that we are creating an instance of Dog, which means the Animal class is now dependent on Dog class, only then, we’re able to successfully create an instance and assign it to the dog variable.

class Animal{

private Dog dog = new Dog();
}

But in Spring, we just need to specify that we need a dependency Dog and spring creates the object for us. Our job is simply to mention the dependency and spring does the rest.

Class Animal{
@Autowired
private Dog dog;
}

So, you can see how we have inverted the control just by providing the dependency. The spring framework takes the responsibility of creating it and injecting it into the component. This whole idea is termed as Inversion of Control.

Spring Architecture

Although spring is a large framework it is broken down into separate modules that helps developer to choose for their use case. Spring is built in a modular way that means we have the ability to choose only the modules we want not the entire framework.

Data Access/Integration

Spring contains modules like JDBC, ORM, OXM, JMS, and Transactions. It has very good integration data and Integration layers and provides support to interact with databases.

  • The JDBC (Java Database Connectivity) in Spring allows the data access layer to connect to the database and get data from it. There’s no need for any JDBC code. The spring version of JDBC is much shorter than the regular JDBC code.
  • The Spring ORM (Object Relation Mapping) module has integration with ORM frameworks like JPA and Hibernate providing great support.
  • The OXM (Object XML Mapping) module provides all the required features in converting the object to XML format.
  • The JMS (Java Messaging Service) module provides great connectivity to other applications with the help of queue to consume or produce messages.
  • If any transaction fails the Transaction management module provides support.

Web (MVC/Remoting)

Spring has a separate web framework called SpringMVC.

In support of creating Web Applications, spring has Web, Servlets, Portlets, and Sockets modules.

AOP

Spring has its own AOP (Aspect Oriented Programming) that offers basic aspect-oriented programming functionality like method interception, pointcuts as well as security and logging features.

Spring Core Container provides the fundamental functionality of the spring framework. Inversion of Control (IoC), dependency Injection, are all provided by the spring core container.

Spring has come up with a solution to all the enterprise application problems by introducing Spring Projects that can be used based on the use case.

Some of the most commonly used Spring Projects are:

  • Spring Boot
  • Spring Cloud
  • Spring Data
  • Spring Integration
  • Spring Batch
  • Spring Security
  • Spring Android.

Conclusion

Starting to learn Spring might look tough in the beginning, but once you choose to learn the Spring framework it gets interesting and exciting. Spring is a very huge framework and is still in the market. It is always good to at least know how it is used and maybe incorporate it in your new project if Java is the go-to language you choose.

Happy Learning!

--

--

SitaRamSai
SitaRamSai

Responses (1)