What Is Spring Bean

By | | Updated : 2021-03-09 | Viewed : 11047 times

What Is Spring Bean

The article explains about the Spring bean and its creation process. Here the current article describes What a Spring bean is? How Spring Container creates the Bean? How to configure the Spring Bean in bean.xml?

Spring Bean Introduction

Spring bean is a normal POJO (Plain Old Java Object) class, which is managed by Spring Container. Spring container manage the Spring Bean life cycle, i.e., from instantiation to destruction of the bean. Spring Container creates Spring bean by fetching the bean's meta-data in the beans.xml. Here Spring container use the concept of Dependency Injection to handle dependent bean injection into the dependency bean. Inversion Of Control used by Spring Container, to create the Bean object.

IOC and Dependency Injection

IOC and Dependency Injection are main concepts for Bean instantiation and initialization in Spring.

Inversion Of Control

This is a process which will be used for the creation of Spring Bean instances. Here Spring Bean means regular Java class. Instead of object creation manually, the Spring Container creates the object. Spring container needs metadata for Spring Beans, i.e., beans.xml to object creation. In beans.xml file contains the beans definition.

Dependency Injection

Dependency Injection defines how dependency bean should be injected into the dependent Bean. There Three types of Dependency Injections are available. Those are 1. Constructor Injection 2. Setter Injection 3. Interface Injection. Spring framework uses only Constructor Injection and Setter Injection. Please verify Service-Locator-Pattern about the detailed explanation of IOC and Dependency injection.

Spring Bean Configuration

  • XML Based Configuration

  • Annotation Based Configuration

  • Java Based Configuration

XML Based Configuration

<bean id="aSpringBean" class="com.docsconsole.tutorials.spring5.core.ASpringBean" >
	<property name="beanName" value="aTestedSpringBean"></property>

In the above the beans.xml, we configured ASpringBean bean class within the bean tag element. Here we have provided the id and class properties. These properties should have unique values in beans.xml

Annotation Based Configuration

public class BSpringBean {
	private ASpringBean aSpringBean;
	//getter for aSpringBean
	public void setaSpringBean(ASpringBean aSpringBean) {
		this.aSpringBean = aSpringBean;

Here BSpringBean has dependency as ASpringBean. In the Setter method of ASpringBean, the @Autowired is annotated. Spring Container fetch ASpringBean’s metadata form beans2.xml to create the ASpringBean instance.

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

	<context:annotation-config />

	<bean id="aSpringBean" class="com.docsconsole.tutorials.spring5core.example3.ASpringBean" />
	<bean id="bSpringBean" class="com.docsconsole.tutorials.spring5core.example3.BSpringBean">


Java Based Configuration

public class AppConfig {

	public ASpringBean getASpringBean() {
		ASpringBean aSpringBean = new ASpringBean();
		return aSpringBean;

Here we annotated AppConfig Class with @Configuration annotation. AppConfig Class has methods which are annotated with @Bean. So, Spring container process these methods to generate the Spring Bean Object.

Leave A Reply