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.
- 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
- <?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
- <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
- 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.
Hi KB,
Could you please share more information About alias.
Thanks
actually what is the need for creating beans
Hello KB,
Your way of explanation is very very good and understandable.
What is the use of bean alias? Where do we use this? When do we use bean id and when do we use bean alias?
Hi Mohan,
Main thing to remember in spring is, 2 beans can have same alias but cannot have same id.
Id is always unique to each bean.
Using alias can be useful in a large project having multiple modules where we are not allowed to change the bean definition in some module then we can use alias and override the bean definition as well.
An aliased bean will always have higher priority over a non-aliased one, and in case of having different beans with the same alias then the last one declared will have the priority. In other words, the aliased bean will override the non-aliased beans.
I will write one article on this with detailed example.
Thanks
Hi KB,
Could you please share more information About alias.
Thanks