Master Spring Boot Startup: From static blocks to @PostConstruct and Runner Interfaces

This article explains the various ways to execute code during a Spring Boot application's startup, comparing Java static blocks, constructors, @PostConstruct methods, and Spring's ApplicationRunner and CommandLineRunner interfaces, including ordering with @Order and providing sample code and execution results.

Programmer DD
Programmer DD
Programmer DD
Master Spring Boot Startup: From static blocks to @PostConstruct and Runner Interfaces

Java native startup loading methods

static code block

static block runs when the class is loaded.

Constructor

Runs during object initialization, after the static block.

Spring startup loading methods

@PostConstruct annotation

Method annotated with @PostConstruct executes after dependency injection initialization.

ApplicationRunner and CommandLineRunner

Spring Boot provides two interfaces to run code after the container has started. Both require implementing a run method; the difference lies in the method parameter: ApplicationRunner receives ApplicationArguments, while CommandLineRunner receives a String array.

What is ApplicationArguments

Provides access to the arguments that were used to run a SpringApplication.

It allows retrieving the arguments passed to SpringApplication.run(...) at runtime.

@Order annotation

When multiple classes implement CommandLineRunner or ApplicationRunner, adding @Order on the class sets the execution sequence.

Code test

TestPostConstruct

@Component
public class TestPostConstruct {
    static {
        System.out.println("static");
    }
    public TestPostConstruct() {
        System.out.println("constructer");
    }
    @PostConstruct
    public void init() {
        System.out.println("PostConstruct");
    }
}

TestApplicationRunner

@Component
@Order(1)
public class TestApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        System.out.println("order1:TestApplicationRunner");
    }
}

TestCommandLineRunner

@Component
@Order(2)
public class TestCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {
        System.out.println("order2:TestCommandLineRunner");
    }
}

Execution result

Summary

During Spring application startup, classes annotated with @Component are scanned, their static blocks execute first, followed by constructors, then @PostConstruct methods. After the container starts successfully, the run methods of CommandLineRunner and ApplicationRunner are invoked according to the order defined by @Order.

Therefore, the loading order is: static > constructor > @PostConstruct > CommandLineRunner & ApplicationRunner.

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.

JavaBackend DevelopmentSpring BootApplicationRunnerPostConstructCommandLineRunnerapplication startup
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.