SoFunction
Updated on 2025-03-05

Various implementation methods for automatically loading system configuration after SpringBoot project is started

In Spring Boot projects, the requirements for automatically loading the system configuration cache operation after the project starts:

1. Use CommandLineRunner

CommandLineRunner is an interface that can be used to execute some logical code immediately after a Spring Boot application is started.

Implementation method:

import ;
import ;

@Component
public class SystemConfigLoader implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        // Load the system configuration cache here        ("The project starts and starts loading system configuration...");
        // Simulate load configuration operations        loadSystemConfig();
    }

    private void loadSystemConfig() {
        // Assume that configuration is loaded from the database        ("System configuration loaded successfully!");
    }
}

2. Use ApplicationRunner

ApplicationRunnerandCommandLineRunnerSimilar, but supports receiving oneApplicationArgumentsObject, used to handle incoming parameters more flexibly.

Implementation method:

import ;
import ;
import ;

@Component
public class SystemConfigLoader implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // Load the system configuration cache here        ("The project starts and starts loading system configuration...");
        loadSystemConfig();
    }

    private void loadSystemConfig() {
        // Assume that configuration is loaded from the database        ("System configuration loaded successfully!");
    }
}

3. Use @EventListener to listen for ApplicationReadyEvent

By monitoringApplicationReadyEvent, you can execute logic after Spring Boot completes all startup processes.

Implementation method:

import ;
import ;
import ;

@Component
public class SystemConfigLoader {

    @EventListener()
    public void onApplicationReady() {
        // Load the system configuration after the project starts        ("The project starts and starts loading system configuration...");
        loadSystemConfig();
    }

    private void loadSystemConfig() {
        // Assume that configuration is loaded from the database        ("System configuration loaded successfully!");
    }
}

4. Use @PostConstruct annotation

@PostConstructThe annotation will be executed after the Bean is initialized, but its execution time is earlier than the project is fully started, so it is necessary to cooperate with delay operations to ensure that the project is fully started before execution.

Implementation method:

import ;
import ;

@Component
public class SystemConfigLoader {

    @PostConstruct
    public void init() {
        // Delay loading to ensure the project is fully started        new Thread(() -> {
            try {
                (2000); // Simulation delay                ("The project starts and starts loading system configuration...");
                loadSystemConfig();
            } catch (InterruptedException e) {
                ().interrupt();
            }
        }).start();
    }

    private void loadSystemConfig() {
        // Assume that configuration is loaded from the database        ("System configuration loaded successfully!");
    }
}

5. Use the SmartLifecycle interface

SmartLifecycleProvides more flexible controls that can control the start and stop timing of the code.

Implementation method:

import ;
import ;

@Component
public class SystemConfigLoader implements SmartLifecycle {

    private boolean running = false;

    @Override
    public void start() {
        // Execution logic after project startup is completed        ("The project starts and starts loading system configuration...");
        loadSystemConfig();
        running = true;
    }

    @Override
    public void stop() {
        // Stop logic (optional)        ("Perform cleanup work when the project is stopped...");
    }

    @Override
    public boolean isRunning() {
        return running;
    }

    private void loadSystemConfig() {
        // Simulate load configuration operations        ("System configuration loaded successfully!");
    }
}

Comparison and recommendation

  1. Simple scenario:

    • Recommended useCommandLineRunnerorApplicationRunner, the implementation is simple and clear.
  2. More flexible listening to startup events:

    • Recommended use@EventListenerMonitorApplicationReadyEvent, which ensures that all bean initialization is completed.
  3. More finer granular control is required:

    • useSmartLifecycleProvides more flexible control.

The above is the detailed content of various implementation methods for automatic loading system configuration after the SpringBoot project is started. For more information about SpringBoot automatic loading system configuration, please pay attention to my other related articles!