Fredy  Larson

Fredy Larson


Spring Beans in Depth

Hi guys! Today I want to explore Spring Beans in depth : what is a bean, how you can use it, what bean scopes are and what is the lifecycle of a bean.


The bean is an important concept of the Spring Framework and as such, if you want to use Spring, it is fundamental to understand what it is and how it works. By definition, a Spring bean is an object that form the backbone of your application and that is managed by the Spring IoC container. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.

Inversion of Control (IoC)

Inside Spring, a bean exploits the Inversion of Control feature by which an object defines its dependencies without creating them. This object delegates the job of constructing and instantiating such dependencies to an IoC container, the Spring lightweight container.

For example, if we have a class Person which has a property of class Animalas following :

public class Person {
	    private Animal animal;

	    public Person(Animal animal) {
	        this.animal = animal;

	    // getter, setter

Suppose that Animal has two properties : name and age. To instantiate a Person object normally we have to do the following things :

Animal animal = new Animal("fuffy", 5);
	Person person = new Person(animal);

#programming #java #coding #spring-bean #software-development

Spring Beans in Depth