What are the important bean lifecycle methods? Can they be overloaded?

 In Java, the life cycle of a Bean is managed by the Spring container, and there are some important life cycle methods that can be overloaded during the Bean life cycle. These methods include the following:

  1.Initialization Callbacks:

  ·@PostConstruct: Method called immediately after Bean initialization.

  ·Implement the afterPropertiesSet method of the InitializingBean interface: called after the Bean's properties are set.

  2.Destruction Callbacks:

  ·@PreDestroy: Method called before the Bean is destroyed.

  ·Implement the destroy method of the DisposableBean interface: called when the Bean is destroyed.

1695352429753_Which Java training is better13.jpg

  Next, let's take a look at the detailed description and sample code of these methods:

  1. @PostConstruct和@PreDestroy

  @PostConstruct and @PreDestroy are commonly used annotations for defining initialization and destruction callback methods.

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class MyBean {

    @PostConstruct
    public void init() {
        // 在Bean初始化后调用
        System.out.println("Bean初始化完成");
    }

    @PreDestroy
    public void destroy() {
        // 在Bean销毁前调用
        System.out.println("Bean即将销毁");
    }
}

  2. InitializingBean和DisposableBean

  These interfaces define afterPropertiesSet and destroy methods, which can be overloaded during the Bean's life cycle.

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class MyBean implements InitializingBean, DisposableBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        // 在Bean属性设置完毕后调用
        System.out.println("Bean属性设置完毕");
    }

    @Override
    public void destroy() throws Exception {
        // 在Bean销毁时调用
        System.out.println("Bean销毁");
    }
}

  3. Custom initialization and destruction methods

  In addition to the above standard life cycle callback methods, we can also customize initialization and destruction methods and specify them in the Spring configuration file.

public class MyBean {

    // 自定义初始化方法
    public void customInit() {
        System.out.println("自定义初始化方法");
    }

    // 自定义销毁方法
    public void customDestroy() {
        System.out.println("自定义销毁方法");
    }
}

  In Spring's configuration file:

<bean id="myBean" class="com.example.MyBean" init-method="customInit" destroy-method="customDestroy" />

  In the above example, the init-method attribute is used to specify the initialization callback method, and the destroy-method attribute is used to specify the destruction callback method.

  No matter which method is chosen, the Spring container will call the corresponding method during the bean's life cycle, giving us the opportunity to perform specific logic during initialization and destruction.

Guess you like

Origin blog.csdn.net/Blue92120/article/details/133162986