Introduction of Life Cycle.pptx

fake621211 68 views 5 slides Apr 15, 2023
Slide 1
Slide 1 of 5
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5

About This Presentation

intro of life cycle


Slide Content

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.

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 :- public void init() 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.)

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

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: Firstly, we need to create a bean ,write the init() and destroy() methods in the class. We need to configure the spring XML file need to register the init() and destroy() methods in it.

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.
Tags