Signup/Sign In

Spring Bean

Bean is an object in Spring that is managed by the Spring IoC Container. Spring creates bean with the configuration metadata that we have supplied in the <bean> tag of the XML file. We provide metadata to the IoC container either by using the XML file or by Java annotations.

During metadata configuration, we provide bean definitions with some optional attributes such as:

  • The fully qualified name of Bean class name. such as

  • Bean behavior such as Bean scope, lifecycle callback, etc.

  • Bean dependencies (references to other beans) that are needed for the bean.

The <bean> Tag Structure

<bean id="demoBean" class="com.examples.DemoBean" />

The id attribute sets a unique id for the class specified by the class attribute.

Bean Definition Properties

The following table contains the <bean> tag properties that are used to configure the bean in the configuration file.

Bean Property



This property is used to specify the class for which an object is created.


This property is used to specify an identifier for a bean that is unique.


It specifies the scope of beans such as singleton or prototype.

Constructor arguments

It is used for constructor based dependency injection.


This property is used for property-based dependency injection.

Autowiring mode

It is used to set bean auto wiring.

collaborators and lazy initialization mode

It is used to set lazy bean initialization.

Initialization method

It is used to set the initialization method to execute at bean initialization.

Destruction method

It is used to set destructive methods that execute before destroying of bean object.

1. Naming Bean

To set the name of a bean in XML-based configuration, we use the id, name attributes, or both. The id attribute lets us specify exactly one id.

While setting bean names we are required to follow the naming conventions it means bean names start with a lowercase letter and are camel-cased.

<bean id="..." class="..."> 
    <!-- Configuration for this bean go here --> 

Lazy Initialization

By default, ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process.

A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.

<bean id = "..." class = "..." lazy-init = "true">
      <!-- Configuration for this bean go here -->

Init Method

This attribute is used to specify the method that executes at bean initialization time. Syntax of the bean tag and attribute is given below.

<bean id = "..." class = "..." init-method = "...">
      <!-- Configuration for this bean go here -->

Destruction Method

This attribute is used to specify the method that executes at bean destroy time. Syntax of the bean tag and attribute is given below.

<bean id = "..." class = "..." destroy-method = "...">
      <!-- Configuration for this bean go here -->

Simple Bean Example

Let's create an example to create a bean and access its properties from the bean and property tags. The following are Java and XML files that we created in our project.


This is a simple Java POJO bean class that has setter and getter methods to handle data.

public class Employee {
	int id;
	String name;

	public int getId() {
		return id;

	public void setId(int id) { = id;

	public String getName() {
		return name;

	public void setName(String name) { = name;


This file reads the XML file and gets bean data set in the XML file. We used getter methods to get bean data.


import org.springframework.context.ApplicationContext;

public class SpringApp {

	private static ApplicationContext context;
	public static void main(String[] args) {

		context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Employee e = (Employee) context.getBean("employee");
		System.out.println("Id: "+e.getId());
		System.out.println("Name: "+e.getName());

// applicationContext.xml

This is a context file that configures the bean tag and its properties by using the property tag.

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

	<bean id="employee" class="">
		<property name="id" value="0501"></property>
		<property name="name" value="Rahul"></property>

// pom.xml

This is a maven dependency file that is used to configure all the project settings and dependencies.

<project xmlns=""
		<!-- -->

Run the Application

After successfully completing the project and adding the dependencies run the application and you will get the output as below.

Id: 501
Name: Rahul

About the author:
I am a Java developer by profession and Java content creator by passion. I have over 5 years of experience in Java development and content writing. I like writing about Java, related frameworks, Spring, Springboot, etc.