How Spring Creates Beans Porridge
By: Victor Jo
Tags: Spring Boot, Lifecycle, Spring Framework, Spring, Spring Bean

Photo by Volodymyr Hryshchenko on Unsplash

In this article, I’ll be explaining how Spring creates its Beans, from picking them (or Instantiating the beans) to adding the ingredients (or adding Dependencies i.e. Dependency Injection), then cooking it (or Initializing the beans) and eventually when she decides to throw the delicious beans away (i.e. Beans Destruction 😢). All of these constitute a Spring Bean Life-cycle.

The Porridge Beans Life-cycle

The Spring Bean Life-cycle

According to the Spring Docs:

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.

A Spring Inverse of Control (IoC) container is the chef for this dish. She manages one or more beans. Yes, she can cook a single bean, she’s good like that. Although, the IoC container needs the recipe to create one or more beans. This recipe is the Bean Definition. Whenever a bean is requested of the IoC, she looks at the recipe for that bean and uses what’s called the Configuration Metadata encapsulated by that bean definition to create (or acquire) an actual object.

Picking the Beans and Adding the Ingredients

The configuration metadata can be defined in three(3) ways, all of which the Spring IoC is capable of processing:

XML-Based Configuration Metadata

This configures beans as <bean/> elements inside a top-level <beans/> element. The following example shows the basic structure of XML-based configuration metadata:

<?xml version="1.0" encoding="UTF-8"?>

<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

https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="..." class="...">

<!-- collaborators and configuration for this bean go here -->

</bean> <bean id="..." class="...">

<!-- collaborators and configuration for this bean go here -->

</bean> <!-- more bean definitions go here --> </beans>

The id attribute is a string that identifies the individual bean definition. The class attribute defines the type of the bean and uses the fully qualified class name.

Assuming you have a service layer object and you want to configure it, it’ll look like this in a services.xml configuration file:

<?xml version="1.0" encoding="UTF-8"?>

<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

https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- services --> <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">

<property name="accountDao" ref="accountDao"/>

<property name="itemDao" ref="itemDao"/>

<!-- additional collaborators and configuration for this bean go here -->

</bean> <!-- more bean definitions for services go here --> </beans>

The property name element refers to the name of the JavaBean property, and the ref element refers to the name of another bean definition. Below shows the other bean definition, which are data access objects in a daos.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<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

https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="accountDao"

class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">

<!-- additional collaborators and configuration for this bean go here -->

</bean> <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">

<!-- additional collaborators and configuration for this bean go here -->

</bean> <!-- more bean definitions for data access objects go here --> </beans>

Annotation-based Configuration Metadata

Using annotation-based configuration, which relies on the bytecode metadata for wiring up components instead of angle-bracket declaration (as with XML-based config), you can move the configuration of a bean into the component class itself. These annotations can be applied on the relevant class, method, or field declaration. Basically you would use the @Autowired annotation to specify the dependencies (or ingredients) a particular bean needs to function or be tasty if you like.

You could decide to register the beans individually as in the services.xml without the <property/> tags or you can register them implicitly as follows:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

https://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> </beans>

According to the Spring docs: <context:annotation-config/> only looks for annotations on beans in the same application context in which it is defined. This means that, if you put <context:annotation-config/> in a WebApplicationContext for a DispatcherServlet , it only checks for @Autowired beans in your controllers, and not your services.

Then for the dependencies, instead of the XML config shown in daos.xml you would do this in the component class:

public class PetStoreServiceImpl implements PetStoreService {



private final JpaItemDao itemDao;

private final JpaAccountDao accountDao; @Autowired

public PetStoreServiceImpl(JpaItemDao JpaItemDao, JpaAccountDao, accountDao) {

this.itemDao = itemDao;

this.accountDao = accountDao;

}



// ...

}

Java-based Container Configuration

The central artifacts in Spring’s Java-Configuration support are @Configuration -annotated classes and @Bean -annotated methods.

The @Bean is the java equivalent of using <beans/> XML tag to define a <bean/> . This annotation indicates that the method instantiates, configures, and initializes a new object to be managed by Spring IoC container.

Annotating a class with @Configuration indicates that its primary purpose is as a source of bean definitions. Furthermore, @Configuration classes let inter-bean dependencies be defined by calling other @Bean methods in the same class.

The equivalent of the configuration in the XML-based config for services.xml is as follows:

@Configuration

public class AppConfigurations {



@Bean

public PetStoreService myPetStoreService() {

return new PetStoreServiceImpl();

}

}

The above has discussed the various ways of telling the Spring IoC how to pick (or instantiate) a delicious bean(s) and add its ingredients (or dependencies).