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

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. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
  6.  
  7.     <bean id="person" class="com.kb.di.Person">
  8.      
  9.         <property name="id" value="11"/>
  10.         <property name="name" value="Raj1"/>
  11.         <property name="age" value="25"/>
  12.  
  13.     </bean>
  14. </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. <alias name=”person” alias=”personAlias”/>
  2. <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. package com.kb.beans;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6.  
  7. public class BeansExample {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");  
  12.         Person person = (Person)applicationContext.getBean("person");
  13.         System.out.println("Id : "+person.getId());
  14.         System.out.println("Name : "+person.getName());
  15.         System.out.println("Age :"+ person.getAge());
  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());
	}
}

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

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