What are spring beans?

Spring beans are the objects which are created and managed completely by spring container.

These beans are the heart of the application.

Beans can be defined in spring either by using XML configuration or by using Annotation.

In XML configuration, bean can be defined using < bean > tag inside < beans > tag.

In Annotation configuration , bean can be defined using the annotations like @Component,@Service,@Controller,@Repository on top of the class definition.

We can also achieve beans definition completely in java class by using java configuration.

Let us see how we define the bean in xml configuration

In our application, let us say we have a class called Person.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.kb.di;
 
public class Person {
    private int id;
    private String name;
    private int age;
    
 
    public void setId(int id) {
        this.id = id;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
 
    public int getId() {
        return id;
    }
 
    public String getName() {
        return name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
}
package com.kb.di;

public class Person {
	private int id;
	private String name;
	private int age;
	

	public void setId(int id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}


	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

How we can make it as bean so that it is completely managed by Spring ?

see below xml configuration file to achieve the same

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
 
    <bean id="person" class="com.kb.di.Person">
     
        <property name="id" value="11"/>
        <property name="name" value="Raj1"/>
        <property name="age" value="25"/>
 
    </bean> 
</beans>
<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

	<bean id="person" class="com.kb.di.Person">
	 
		<property name="id" value="11"/>
		<property name="name" value="Raj1"/>
		<property name="age" value="25"/>

	</bean>	
</beans>

Each bean defined in the spring will have a unique identifier.

Now person object (also called person bean) is going to be created by spring container, and managed by spring container.

Application can use this bean without worrying about creating this object, application also does not need to worry about destroying this object/bean after the use.

Each bean is getting identified by its id.

Beans in spring cannot have more than one id but we can give more than one alias to the bean as below

1
2
<alias name=”person” alias=”personAlias”/>
<bean id="person" class="com.kb.di.Person">
<alias name=”person” alias=”personAlias”/>
<bean id="person" class="com.kb.di.Person">

First parameter inside < alias > tag is alias name, it should be same as the bean id

Second parameter inside < alias > tag is alias, it can be any new name which we want to create as alias.

Now in our application, we can use either person or personAlias to use this bean.

How we use the beans in our application?

We can use this bean in our application either by injecting it to another bean or by loading this bean using spring container.

Let us see below how we can load it using spring container as below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.kb.beans;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 
public class BeansExample {
 
    public static void main(String[] args) {
 
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");  
        Person person = (Person)applicationContext.getBean("person");
        System.out.println("Id : "+person.getId());
        System.out.println("Name : "+person.getName());
        System.out.println("Age :"+ person.getAge());
    }
}
package com.kb.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class BeansExample {

	public static void main(String[] args) {

        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");  
        Person person = (Person)applicationContext.getBean("person");
        System.out.println("Id : "+person.getId());
        System.out.println("Name : "+person.getName());
        System.out.println("Age :"+ person.getAge());
	}
}

So now we have loaded the person bean using its id and we can use it without worrying about its creation.

Now this bean is created by spring and all the values to this bean have been injected by spring container based on our XML configuration.

Now run the above program and we will see the output as below

Key Note: Any class can be called as bean in Spring if it is defined either in the spring xml file or defined by using annotation.

Key advantage of making any class as spring bean is that, its entire life cycle is managed by Spring container and it helps us to achieve decoupling.

How decoupling is achieved ?.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on