Blog Image

Bean Lifecycle  

Spring support both the way of initialization i.e. default and dynamic initialization.

i.e. constructor initialization and setter initialization. Even though spring not supporting java lifecycle.

Why spring has different lifecycle? Why spring not use java lifecycle ?

There are some situation programmer wanted to do some operation with constructor passes value and setter passed value then by using java lifecycle they can not do.

spring also has two attribute which will take care of executing the method after creating the object.

Init-method

Destroy method

In spring we can give any name to the method but we have to add that addition configuration into spring bean configuration file.

Example:

package com.sjg.bean;

public class Math {

    private int i;
    private int j;
    private int sum;

    public Math(int j) {
        this.j = j;
        this.sum = this.i + this.j;
    }

    public void setI(int i) {
        this.i = i;
    }

    public void init() {
        this.sum = this.i + this.j;

    }

    @Override
    public String toString() {
        return "Math [i=" + i + ", j=" + j + ", sum=" + sum + "]";
    }

    public void destroy() {
        System.out.println("shutdown");
    }
}


application-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">;

    <bean id="math" class="com.sjg.bean.Math" init-method="init"
        destroy-method="destroy">
        <constructor-arg value="30" />
        <property name="i" value="20" />
    </bean>
</beans>

Test Class:

public class App {
    public static void main(String[] args) {
        BeanFactory foctory = null;

        foctory = new XmlBeanFactory(new ClassPathResource("com/sjg/common/application-context.xml"));

        Math math = (Math) foctory.getBean("math", Math.class);
        System.out.println(math);
    }
}


About author

User Image
VENKYREDDY

I am a hard working, honest individual. I am a good timekeeper, always willing to learn new skills. I am friendly, helpful and polite, have a good sense of humour. I am able to work independently in busy environments and also within a team setting. I am outgoing and tactful, and able to listen effectively when solving problems.

3

-Comments

Be the first person to write a comment for this Blog
  • author image
    by:
      Manjunatha1
      13-12-2017 10:25:25 AM

    this is a nice blog and good information helping a lot and sharing knowledge good thank you.please share more blogs helping us.

  • author image
    by:
      PramodRanjan2323
      24-4-2017 07:05:50 AM

    this is nice blog There are some situation programmer wanted to do some operation with constructor passes value and setter passed value then by using java lifecycle they can not do.

  • author image
    by:
      Hemanth4145
      21-4-2017 12:53:11 AM

    Good information about spring lifecycle and purpose of lifecycle

Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.