Mastering Spring’s Lifecycle: How SmartLifecycle Simplifies Bean Management

This article explains Spring's Lifecycle and SmartLifecycle interfaces, shows how to implement custom beans that react to container start and stop events, provides complete code examples, and demonstrates the automatic startup and ordered shutdown behavior of SmartLifecycle in a Spring Boot application.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Mastering Spring’s Lifecycle: How SmartLifecycle Simplifies Bean Management

Lifecycle Interface

The Lifecycle interface allows a bean to execute initialization logic after all beans are created and to perform cleanup when the application shuts down.

public interface Lifecycle {
    void start();
    void stop();
    boolean isRunning();
}

Custom Lifecycle Implementation

@Component
public class MyLifeCycle implements Lifecycle {
    private volatile boolean running = false;

    @Override
    public void start() {
        System.out.println("容器启动后执行MyLifeCycle操作...");
        running = true;
    }

    @Override
    public void stop() {
        System.out.println("收到关闭容器的信号MyLifeCycle操作...");
        running = false;
    }

    @Override
    public boolean isRunning() {
        System.out.println("检查MyLifeCycle组件的运行状态:" + running);
        return running;
    }
}

When this class is added to a Spring Boot project, the start() method is not invoked automatically because AbstractApplicationContext#start is never called; only stop() runs during shutdown, and isRunning() is used to decide whether to call stop().

SmartLifecycle Interface

SmartLifecycle

extends Lifecycle and Phased, providing automatic startup and the ability to order multiple lifecycle beans.

public interface SmartLifecycle extends Lifecycle, Phased {
    int DEFAULT_PHASE = Integer.MAX_VALUE;

    default boolean isAutoStartup() { return true; }

    default void stop(Runnable callback) {
        this.stop();
        callback.run();
    }

    default int getPhase() { return Integer.MAX_VALUE; }
}

SmartLifecycle Implementation Example

@Component
public class MySmartLifecycle implements SmartLifecycle {
    private volatile boolean running = false;

    @Override
    public boolean isAutoStartup() { return true; }

    @Override
    public void start() {
        System.out.println("MySmartLifecycle容器启动完成 ...");
        running = true;
    }

    @Override
    public void stop(Runnable callback) {
        System.out.println("MySmartLifecycle容器停止,执行回调函数");
        stop();
        callback.run();
    }

    @Override
    public void stop() {
        System.out.println("MySmartLifecycle容器停止 ...");
        running = false;
    }

    @Override
    public boolean isRunning() {
        System.out.println("MySmartLifecycle检查运行状态 ...");
        return running;
    }

    @Override
    public int getPhase() { return 0; }
}

Running a Spring Boot application with this bean prints start‑up logs, and shutting down prints stop logs and the callback execution. Because isAutoStartup() returns true, the start() method runs without an explicit call to ApplicationContext.start(). The getPhase() value determines ordering: beans with smaller phase values start earlier and stop later.

Summary

Implementing SmartLifecycle lets developers hook into the Spring container lifecycle for custom start‑up and shutdown logic, with automatic startup and ordered execution, making it preferable to the plain Lifecycle interface for most real‑world scenarios.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaspringLifecycleSpringBootSmartLifecycle
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.