Spring 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, for example, in the form of XML <bean/>
definitions which you have already seen in previous chapters.
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
All the above configuration metadata translates into a set of the following properties that
make up each bean definition.
Properties Description
1. class This attribute is mandatory and specify the bean class to be used to create the
bean.
2. 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).
3. scope This attribute specifies the scope of the objects created from a particular bean
definition.
4. constructor-arg This is used to inject the dependencies
5. properties This is used to inject the dependencies
6. autowiring mode This is used to inject the dependencies.
7. 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.
8. initialization method A callback to be called just after all necessary properties on the
bean have been set by the container.
9. destruction method A callback to be used when the container containing the bean
is destroyed.
Spring Bean Scopes
When defining a <bean> in Spring, you have the option of declaring a scope for that bean. For
example, To force Spring to produce a new bean instance each time one is needed, you should
declare the bean's scope attribute to be prototype. Similar way if you want Spring to return
the same bean instance each time one is needed, you should declare the bean's scope
attribute to be singleton.
The Spring Framework supports following five scopes, three of which are available only if you
use a web-aware ApplicationContext.
Scope Description
1. singleton This scopes the bean definition to a single instance per Spring IoC
container (default).
2. prototype This scopes a single bean definition to have any number of object
instances.
3. request This scopes a bean definition to an HTTP request. Only valid in the
context of a web-aware Spring ApplicationContext.
4. session This scopes a bean definition to an HTTP session. Only valid in the context of a
web-aware Spring ApplicationContext.
5. global-session This scopes a bean definition to a global HTTP session. Only valid in the
context of a web-aware Spring ApplicationContext.
Spring Bean Life Cycle
The life cycle of a Spring bean is easy to understand. When a bean is instantiated, it may be
required to perform some initialization to get it into a usable state. Similarly, when the bean
is no longer required and is removed from the container, some cleanup may be required.
Though, there is lists of the activities that take place behind the scenes between the time of
bean Instantiation and its destruction, but this chapter will discuss only two important bean
lifecycle callback methods which are required at the time of bean initialization and its
destruction.
To define setup and teardown for a bean, we simply declare the <bean> with init-method
and/or destroy-method parameters. The init-method attribute specifies a method that is to
be called on the bean immediately upon instantiation. Similarly, destroy-method specifies a
method that is called just before a bean is removed from the container.
Initialization callbacks:
The org.springframework.beans.factory.InitializingBean interface specifies a single method:
void afterPropertiesSet() throws Exception;
So you can simply implement above interface and initialization work can be done inside
afterPropertiesSet() method as follows:
public class ExampleBean implements InitializingBean {
public void afterPropertiesSet() {
// do some initialization work
}
}
In the case of XML-based configuration metadata, you can use the init-method attribute to
specify the name of the method that has a void no-argument signature. For example:
<bean id="exampleBean"
class="examples.ExampleBean" init-method="init"/>
Following is the class definition:
public class ExampleBean {
public void init() {
// do some initialization work
}
}
Destruction callbacks
The org.springframework.beans.factory.DisposableBean interface specifies a single method:
void destroy() throws Exception;
So you can simply implement above interface and finalization work can be done inside
destroy() method as follows:
public class ExampleBean implements DisposableBean {
public void destroy() {
// do some destruction work
}
}
In the case of XML-based configuration metadata, you can use the destroy-method attribute
to specify the name of the method that has a void no-argument signature. For example:
<bean id="exampleBean"
class="examples.ExampleBean" destroy-method="destroy"/>
Following is the class definition:
public class ExampleBean {
public void destroy() {
// do some destruction work
}
}
If you are using Spring's IoC container in a non-web application environment; for example, in
a rich client desktop environment; you register a shutdown hook with the JVM. Doing so
ensures a graceful shutdown and calls the relevant destroy methods on your singleton beans
so that all resources are released.
It is recommended that you do not use the InitializingBean or DisposableBean callbacks,
because XML configuration gives much flexibility in terms of naming your method.
Spring Bean Definition Inheritance
A bean definition can contain a lot of configuration information, including constructor
arguments, property values, and container-specific information such as initialization method,
static factory method name, and so on.
A child bean definition inherits configuration data from a parent definition. The child
definition can override some values, or add others, as needed.
Spring Bean definition inheritance has nothing to do with Java class inheritance but
inheritance concept is same. You can define a parent bean definition as a template and other
child beans can inherit required configuration from the parent bean.
When you use XML-based configuration metadata, you indicate a child bean definition by
using the parent attribute, specifying the parent bean as the value of this attribute.