What is the state of Spring to call Lifecycle start / stop hooks?

I am trying to understand the logic of the interface Lifecycle. The documentation for Lifecyclestates:

Containers will propagate start / stop signals to all components that are used in each container, for example. for a stop / restart script at runtime.

But cantainer doesn't seem to call these methods (start / stop) at all.

For example, the result for the following code snippet is just one output "→ call: running: false"

@Configuration
public class TestApp implements Lifecycle {

    boolean runStatus = false;

    @Override
    public void start() {
        System.err.println(">> call: start (Lifecycle)");
        runStatus = true;
    }

    @Override
    public void stop() {
        System.err.println(">> call: stop (Lifecycle)");
        runStatus = false;
    }

    @Override
    public boolean isRunning() {
        System.err.println(">> call: is running: " + runStatus);
        return runStatus;
    }

    public static void main(String[] args) {
        AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(TestApp.class);
        ctx.stop();
    }
}

PS I heard about SmartLifecycle, and it works great. But I am wondering how we can properly use the start / stop methods from Lifecycle.

+4
1

start() stop() .

@Configuration
 public class TestApp implements Lifecycle {

  boolean runStatus = false;

  public TestApp (){}


  @Bean
  public TestApp testApp(){
    return new TestApp();
  }

  @Override
  public void start() {
    System.err.println(">> call: start (Lifecycle)");
    runStatus = true;
  }

  @Override
  public void stop() {
    System.err.println(">> call: stop (Lifecycle)");
    runStatus = false;
  }

  @Override
  public boolean isRunning() {
    System.err.println(">> call: is running: " + runStatus);
    return runStatus;
  }

  public static void main(String[] args) {
    AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(TestApp.class);
    ctx.start();
    TestApp ta = ctx.getBean(TestApp.class);
    ctx.stop();
  }
}
+1

All Articles