Dark Dubai Call Girls O525547819 Skin Call Girls Dubai
Introduction of Life Cycle.pptx
1. Introduction
Java Bean vs Basic Java Class
A bean is java class,but a java class does not have a bean.
A Java Bean is a java class that should follow following conventions:-
1.It should have a default constructor.
2.It should have a serializable.
3.It should provide methods to set and get the values of the properties, known as
getter and setter methods.
Why use Java Bean ?
It is reusable software component.
A bean encapsulates many objects into one object, so we can access this object from multiple places.
Morever,it provides the easy maintenance.
2. Bean Life Cycle
The life cycle of a spring bean is easy to understand.
When a beam is instantiated,it may be required to perform some initialization to get it into a usable state.
Similarly,when the beam is no longer required and is removed from the container,some cleanup may be required.
Life Cycle Methods :-
I) public void init()
II) public void destroy()
I) You can add custome code during bean initialization
• Calling custom business logic methods.
• Setting up handles to resources (db,sockets,file,etc.)
II) You can add custome code during bean destruction
• Calling custom business logic methods.
• Clean up handles to resources (db,sockets,files,etc.)
3. Bean Life Cycle
We can choose custom method name instead of init() and destroy(). Here, we will use init() method to execute all its code as the
spring container starts up and the bean is instantiated, and destroy() method to execute all its code on closing the container.
Bean Life Cycle Process Flow
4. Ways to implement the Life Cycle of Bean
There are 3 ways to Implements:-
[1]By XML
[2]By Programmatic Approach
[3]Using Annotations
[1]By XML
In this approach, in order to avail custom init() and destroy() method for a bean we have to register these two
methods inside Spring XML configuration file while defining a bean. Therefore, the following steps are followed:
a) Firstly, we need to create a bean ,write the init() and destroy() methods in the class.
b) We need to configure the spring XML file need to register the init() and destroy() methods in it.
5. Ways to Implement the Life Cycle Bean
[2]BY Programmatic Approach:-
To provide the facility to the created bean to invoke custom init() method on the startup of a spring container and to invoke the
custom destroy() method on closing the container,
We need to implement our bean with two interfaces namely InitializingBean, DisposableBean and will have to override
afterPropertiesSet() and destroy() method.
afterPropertiesSet() method is invoked as the container starts and the bean is instantiated whereas, the destroy() method is
invoked just after the container is closed.
[3]Using Annotations
To provide the facility to the created bean to invoke custom init() method on the startup of a spring container and to invoke the
custom destroy() method on closing the container.
We need annotate init() method by @PostConstruct annotation and destroy() method by @PreDestroy annotation.