Java ProgrammingLearn About Bean Definition in Spring Framework

Learn About Bean Definition in Spring Framework

In this chapter, we are going to take a look at the concept of Beans in the spring framework. In a spring application, the objects are managed through the spring IOC container which are known as beans. The container is responsible for the objects creation, wiring the objects together, configuring these objects and handling the entire life cycle of these objects from their creation until they are completely destroyed. The IOC container uses the POJO class and the configuration metadata that we supply to the container in the form XML or annotation metadata to create beans and the container further manages the lifecycle of these beans.

A Bean definition contains the following piece of information called configuration metadata, which helps the container know the following things.

• The way a bean should be created.
• Life cycle details of a bean.
• Associated Bean dependencies.

The above metadata for the bean configuration is provided as a set of properties or attributes in an XML file (configuration file) which together prepare a bean definition. The following are the set of properties.

S No.

Properties

Description

1.

class

In a bean definition, it is a mandatory attribute. It is used to specify the bean class which can be used by the container to create the bean.

2.

name

In a bean definition, this attribute is used to specify the bean identifier uniquely. In XML based configuration metadata for a bean, we use the id and/or name attributes in order to specify the bean identifier(s).

3.

scope

This attribute is used to specify the scope of the objects which are created from a particular bean definition.

4.

constructor-arg

In a bean definition, this attribute is used to inject the dependencies.

5.

properties

In a bean definition, this attribute is used to inject the dependencies.

6.

autowiring mode

In a bean definition, this attribute is used to inject the dependencies.

7.

lazy-initialization mode

In a bean definition, a lazy-initialized bean informs the IoC container to create a bean instance only when it is first requested, instead of startup.

8.

initialization method

In a bean definition, a callback to be called after all required properties on the bean have been set up by the container.

9.

destruction method

In a bean definition, a callback to be used when the container that contains the bean is destroyed.

Configuration Metadata for Bean in Spring Framework
Inside the Spring IOC container, the source of the instructions to the objects are decoupled from the format in which configuration metadata is written. The information fetched from the configuration metadata is used for objects instantiation, configuration and their assembly. The configuration metadata for the Spring IOC container can be specified in the following three ways.
• XML based configuration file.
• Annotation-based configuration.
• Java-based configuration.

In the following example, we are going to look into an XML based configuration file which has different bean definitions. The definitions include lazy initialization (lazy-init), initialization method (init-method), and destruction method (destroy-method) as shown below. This configuration metadata file can be loaded either through BeanFactory or ApplicationContext approach as we discussed in the last chapter.

<?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
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- A simple bean definition -->
   <bean id = "..." class = "...">
      <!— Here will be collaborators and configuration for this bean -->
   </bean>

   <!-- A bean definition which has lazy init set on -->
   <bean id = "..." class = "..." lazy-init = "true">
      <!-- Here will be collaborators and configuration for this bean -->
   </bean>

   <!-- A bean definition which has initialization method -->
   <bean id = "..." class = "..." init-method = "...">
      <!-- Here will be collaborators and configuration for this bean -->
   </bean>

   <!-- A bean definition which has destruction method -->
   <bean id = "..." class = "..." destroy-method = "...">
      <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- more bean definitions can be written below -->
   
</beans>

Conclusion
In this chapter, we discussed about the Beans concept in the spring framework. We have learnt about the various set of properties or attributes which are passed to the IOC container as configuration metadata in the form of XML where this data is utilized by the container to create objects and manage the lifecycle of those objects from their creation until they are destroyed by the container when the task is completed. In this chapter, we just focused on the XML based configuration file to pass the metadata to container, but in the coming chapters we will be working on the spring examples by using Annotation Based Configuration method to configure beans through Java Annotations instead of XML file.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -