What is Spring Bean?
In the earlier article, we have seen what spring dependency is and how spring container is performing spring dependency injection. Now in this article, we will see what is Spring Bean in Spring.
As we all know Spring framework/Spring Boot is the most popular web development framework. In spring all the objects are treated as Beans.
Bean is an object that is instantiated, assembled, and managed by the spring IOC container. A simple java object which is created, configured, and managed by the spring container is called a bean.
Spring can manage one or more beans. Beans are configured using metadata provided to the spring container.
There are 4 different ways of configuring metadata
1. XML-based approach.
2. Annotation-driven configuration.
3. Using java code configuration.
4. Spring boot AutoDriven configuration.
We have already seen this configuration in this post. configuration article
I am taking an example of XML-based configuration as below:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Configuring dependent bean-->
<bean id="student" class="src.model.Student"/>
<bean id="course" class="src.model.Course"/>
<!-- configuration target bean-->
<bean id="university" class="src.University">
<constructor-arg ref="student"/>
<constructor-arg ref="course"/>
</bean>
</beans>
Spring Bean will have the following metadata so that the configuration, instantiation, and management of beans are identified by spring container:
1. id/name
2. class
3. constructor-arguments
4. properties
5. scope
6. auto wiring
7. lazy-initialization mode
8. initialization method
9. destruction method
1. Name:
In Spring bean can be identified by some name so that it performs dependency injection or creation of objects. If you have not provided any id or name spring automatically creates one unique name to identify the bean.
If you want to provide a name you can give it with an id or name attribute in the bean tag. you can give multiple names also with alias attribute.
<bean id="student" alias="studentObject" />
or
<bean name="student" alias="studentObject" />
2. Class:
Spring while creating a bean it looks for the type of bean it should create for a particular bean Id, then it checks its configuration file for the type or class of object that it is to be created.
<!-- Configuring dependent bean-->
<bean id="student" class="src.model.Student"/>
<bean id="course" class="src.model.Course"/>
In the above XML file we are mentioning bean named with the student should be of src.mode.Student class.
3. Constructor arguments
We have seen this in Dependency Injection where we will use constructor injection to inject the dependencies into the target object.
In this we provide a tag <constructor-arg ref=” “/>
<!-- configuration target bean-->
<bean id="university" class="src.University">
<constructor-arg ref="student"/>
<constructor-arg ref="course"/>
</bean>
In the above code, the University bean is dependent on the student bean and course bean where they are injected using constructor injection using the <constructor-arg ref=” “/> tag
4. Properties:
We have seen the same in the Dependency injection part where the dependent objects are injected into the target object using <property /> tag.
<!-- configuration target bean-->
<bean id="university" class="src.University">
<property name="student" ref="studentBean"/>
<property name="course" ref="courseBean"/>
</bean>
In the above code, the university object is dependent on the student and course and we are using setter injection to inject the dependent beans.
5. Scope:
Spring will be able to inject a particular type of object to particular target objects based on the scope of objects created for a particular type. We can configure the bean scope using the configuration metadata instead of the default java class level scope.
Spring supports 5 different types of scopes:
1. Singleton (Default)
2. Prototype
3. Request
4. Session
5. Global Session
6. Auto Wiring:
Spring can auto resolve the dependencies using the configuration provided to the spring container.
Spring will reduce the need of constructor arguments when we use auto wiring in the configuration using XML based.
The auto wiring functionality has 4 modes:
1. No (Default)
2. byName
3. byType
4. constructor
Note :- We will see Autowiring and Scopes in detail in next articles.
7. lazy-initialization mode
There is a requirement where we don’t want all the beans to be instantiated but when we require that specific bean spring container should provide that. we can achieve this using lazy-init=”true” attribute.
Lazy-initialized beans are beans in a Spring context that are not instantiated immediately when the context is created but are instead instantiated when they are first requested.
<!-- Configuring dependent bean-->
<bean id="student" class="src.model.Student" lazy-init="true"/>
<bean id="course" class="src.model.Course"/>
In the above code, the student object will not be created at the startup but when we try to create an object which is dependent on this, Spring will create a bean for this student object.
8. initialization method
9. destruction method
we will see this in the upcoming articles when we see the lifecycle of a bean.
Please do follow for more articles and learn with me.
Special Thanks to Nitin M who made these all concepts very easy.