Spring Framework Bean Definition
The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.
A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.
These beans are created with the configuration metadata that you supply to the container.
The bean definition contains the information called configuration metadata which is needed for the container to know the followings:
How to create a bean
Bean's lifecycle details.
Bean's dependencies.
Bean Properties
class
This attribute is mandatory and specify the bean class to be used to create the bean.
name
This attribute specifies the bean identifier uniquely. In XML-based configuration metadata, you use the id and/or name attributes to specify the bean identifier(s).
scope
This attribute specifies the scope of the objects created from a particular bean definition and it will be discussed in bean scopes chapter.
constructor-arg
This is used to inject the dependencies and will be discussed in next chapters.
properties
This is used to inject the dependencies and will be discussed in next chapters.
autowiring mode
This is used to inject the dependencies and will be discussed in next chapters.
lazy-initialization mode
A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.
initialization method
A callback to be called just after all necessary properties on the bean have been set by the container. It will be discussed in bean life cycle chapter.
destruction method
A callback to be used when the container containing the bean is destroyed. It will be discussed in bean life cycle chapter.
Spring Configuration Metadata
Spring IoC container is totally decoupled from the format in which this configuration metadata is actually written.
There are following three important methods to provide configuration metadata to the Spring Container:
XML based configuration file.
Annotation-based configuration.
Java-based configuration.
Note:
You already have seen how XML based configuration metadata provided to the container,
but let us see another sample of XML based configuration file with different bean definitions including lazy initialization,
initialization method and destruction method: