Spring(bean life cycle management), Study notes of Applications of Computer Sciences

this notes describes shortly about the spring bean life cycle management technique

Typology: Study notes

2016/2017

Uploaded on 10/06/2017

suresh-kamble
suresh-kamble 🇮🇳

5

(2)

8 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
07 - Spring Bean Life Cycle
TOC
06 - Spring Bean Scopes
08 - Spring BeanPost Processors
7.1 Overview of Spring Bean Life Cycle
Life of traditional java objects starts on calling new operator which instantiates the object
and finalize() method is getting called when the object is eligible for garbage collection.
Life cycle of Spring beans are different as compared to traditional java objects.
Spring framework provides the following ways which can be used to control the lifecycle
of bean:
1. InitializingBean and DisposableBean callback interfaces
2. Bean Name, bean factory and Application Context Aware interfaces for specific
behavior
3. custom init() and destroy() methods in bean configuration file
For annotation based configurations -
@PostConstruct and @PreDestroy annotations
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Spring(bean life cycle management) and more Study notes Applications of Computer Sciences in PDF only on Docsity!

07 - Spring Bean Life Cycle

TOC

  • 06 - Spring Bean Scopes
  • 08 - Spring BeanPost Processors

7.1 Overview of Spring Bean Life Cycle

Life of traditional java objects starts on calling new operator which instantiates the object

and finalize() method is getting called when the object is eligible for garbage collection.

Life cycle of Spring beans are different as compared to traditional java objects.

Spring framework provides the following ways which can be used to control the lifecycle

of bean:

1. InitializingBean and DisposableBean callback interfaces

2. Bean Name, bean factory and Application Context Aware interfaces for specific

behavior

3. custom init() and destroy() methods in bean configuration file

For annotation based configurations -

@PostConstruct and @PreDestroy annotations

Below diagram shows the complete lifecycle methods (from instantiate to Ready To use

Following diagram shows the method calling at the time of destruction.

7.2 InitializingBean and DisposbleBean callback interfaces

@Override public void destroy() throws Exception { System.out.println("destroy method of person bean is called !! "); } @Override

public void afterPropertiesSet() throws Exception { called !! ");System.out.println("afterPropertiesSet method^ of^ person^ bean is } public String getName() { return name; } public void setName(String name) { this.name = name; } }

b) Create a beans.xml file in src directory to define the PersonBean

<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">

c) Create TestPersonBean class which will just loads the beans.xml and test the person

bean life cycle

import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestPersonBean { public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); ("personBean");PersonBean bean =^ (PersonBean)context.getBean System.out.println(bean.getName()); ((AbstractApplicationContext) context).registerShutdownHook (); }

We can configure the default init-method and destroy-method which will be applied on

all the beans .They are useful when we have a pattern of defining common method

names such as init() and destroy() for all your beans consistently.

7.4.1 Example

Write and example to show the init-method and destroy-method

Solution

a)Write a class CustomLifeCycleMehodBean

public class CustomLifeCycleMethodBean { private String name;

public CustomLifeCycleMethodBean() { System.out.println("Constructor of bean is called !! "); }

public void customDestroy() throws Exception {

called !! ");^ System.out.println("custom^ destroy method^ of^ bean^ is

public void customInit() throws Exception { System.out.println("custom Init method of bean is called !! "); }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

b) Create a beans.xml file in src directory to define the CustomMethodLifeCycleBean

d)Run the Program

You will see below output and custom life cycle methods are getting called

7.4.2 – Example

Write an example to demonstrate global init and destroy methods

Solution

a)Write a class CustomGlobalLifeCycleMehodBean

public class CustomGlobalLifeCycleMehodBean { public CustomGlobalLifeCycleMehodBean() { System.out.println("Constructor of bean is called !! "); } public void globalCustomDestroy() throws Exception { called !! ");System.out.println("global^ custom^ destroy^ method^ of^ bean^ is } public void globalCustomInit() throws Exception { System.out.println("global custom Init method of bean is called !! ");

b) Create a beans.xml file in src directory to define the

CustomGlobalMethodLifeCycleBean

c) Create TestCustomMethodLifeCycleBean class which will just loads the beans.xml

and test the custom methods life cycle

import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestCustomGlobalMethodLifeCycleBean {