Spring Terminology

When we start the Spring Framework, we start listening terms like Beans, Autowiring, Dependency Injection, IoC Factory, Inversion of Control, Application Context, etc. So we need to understand them before to go in full with Spring.

Beans

The Beans are the objects that are created and managed by Spring. Spring manage all the dependencies of an object and instantiates the object after injecting the required dependencies.

A Bean is defined the most of the times with the annotation @Component in above the class declaration..

@Component
public class Phone {

  BlackCoffee blackCoffe = new BlackCoffee();

  //.. More methods and variables
}

The less common but it a good way to do it is using the XML Configuration, using the Spring configuration file applicationContext.xml. Inside of the XML file we use the <bean> tag.

<beans
    xmlns
="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="myBean" class="com.example.MyBean"/>
</beans>

Autowiring

In Spring autowiring means to the process of identifying a dependency or look for a bean that match, and then populating the dependency. Using autowiring removes the wiring the dependencies manually in the XML Configuration.

For this process is used the @Autowired annotation. This annotation tells Spring to find and inject a bean into another. The are different modes of autowiring available in Spring, such as byType, byName, constructor and autodetect. Spring recommend to use autowiring byType.

In the next scenario, we have a bean CoffeeMaker, and other bean Cappuccino that implements the Coffee interface.

@Component
public class
CoffeeMaker {
@Autowired
  Coffee coffee;
  //.. More methods and variables
}

@Component
public class
Cappuccino implements Coffee {

}

Spring will look for a bean Cappuccino and inject the dependency in the coffee reference.

Dependency Injection

Dependency injection is a technique or approach, is a design principle that aims to enhance modularity, flexibility, and maintainability in Software Development, this is the D for SOLID.

In Spring the dependency injection is the process by which Spring looks up the beans that are needed for a particular bean to function and injects them as a dependency. Spring can perform dependency injection by using constructor or by using a setter method. (There are other ways to do it but Spring recommend use these two).

Inversion of Control

The common and traditional approach is for a class to decide when and how to create a dependency. For example, Latte class is a dependency of CoffeeMaker class, which creates its object:

public class CoffeeMaker {

  private Latte latte = new Latte();
  //.. More methods and variables
}

Spring assumes the responsibility of creating and injecting dependencies, “relieving” the class from this task. The developer only needs to declare the dependency, and Spring framework handles the rest.

public class CoffeeMaker {

  private Latte latte;
  //.. More methods and variables
}

After that we can say that Inversion of Control is when the control of creating the dependencies is taken or moved to Spring. So, Spring takes the control for identifying the dependencies of a component, ensuring their availability, and injects them in the component.

In the traditional approach the Class creates their own dependencies, and with the Inversion of control the framework injects the dependencies when they are needed.

IoC Container

An IoC container is a framework that provides the Inversion of Control functionality.

The IoC container manages the beans. Let’s explain using the next classes:

public class CoffeeMaker {

  private Latte latte;
  //.. More methods and variables
}

The IoC creates an instance of Latte class, then creates an instance of CoffeeMaker class, after the beans are created, the framework injects Latte object(bean) as a dependency into the CoffeeMaker object(bean).

The term “IoC container” refers to a generic concept and is not specific to any particular framework. It represents a container or framework that implements the Inversion of Control (IoC) principle. The IoC container is responsible for managing the lifecycle of objects, resolving dependencies, and injecting them into the appropriate components. It acts as a central hub that coordinates the creation, configuration, and assembly of objects within an application. Various frameworks, including Spring, provide their own implementations of IoC containers.

Spring offers two implementations of the IoC container: Bean Factory and Applicationcontext.

Both implementations are interfaces that have a different implementations available. Application Context is the typical IoC container in the Spring, even Spring recommends using it unless there is a memory concern, like in a mobile device or constraints of memory in the project, where is better to use the bean factory.

Bean Factory

This is the basic version of the Spring IoC container. It is the legacy IoC container and provides a basic management for beans and wiring their dependencies. This stills exist due that Spring stills providing backward compatibility.

Application context

This Spring IoC container adds more features to Bean Factory that are typically needed by an enterprise application. Application Context is the most important part of Spring Framework, this is true because all the Spring logic happens here.

Some of the additional features in application context include: Spring AOP features, internationalization, web application context, etc.

Knowing this terminology, makes easy to learn Spring, now you can go for it.

Thanks for reading, and Happy learning!!!

Leave a comment