Preload when SpringBoot project starts

Preload when SpringBoot project starts

Insert image description here

Spring Boot is a popular Java development framework that provides many convenient features to simplify application development and deployment. One of the common requirements is to preload some data or perform some initialization operations when the Spring Boot application starts.

1. CommandLineRunner 和 ApplicationRunner

Spring Boot provides CommandLineRunnerand ApplicationRunnerinterfaces that allow you to execute specific code when your application starts. You can create a Bean that implements these interfaces and runwrite initialization logic in methods. The main difference between these interfaces is runthe type of parameters passed to the methods, you can choose one of them according to your needs.

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyCommandLineRunner implements CommandLineRunner {
    
    
    @Override
    public void run(String... args) throws Exception {
    
    
        // 在这里执行初始化操作
    }
}
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class MyApplicationRunner implements ApplicationRunner {
    
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
    
    
        // 在这里执行初始化操作
    }
}

2. @PostConstruct annotation

You can also use @PostConstructannotations to mark a method that will be automatically called when the Spring container initializes the bean. This is a simpler approach suitable for initialization operations that do not require access to command line parameters or application parameters.

import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

@Component
public class MyInitializer {
    
    
    @PostConstruct
    public void initialize() {
    
    
        // 在这里执行初始化操作
    }
}

3. Implement ApplicationListener

If you need to listen to the application context's initialization events, you can implement ApplicationListenerthe interface. This allows you to define a listener to capture ContextRefreshedEventevents that are fired after the application context initialization is complete.

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class MyContextRefreshedListener implements ApplicationListener<ContextRefreshedEvent> {
    
    
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
    
    
        // 在这里执行初始化操作
    }
}

4. Use @EventListener annotation

In addition to implementing ApplicationListenerinterfaces, you can also use @EventListenerannotations to create event listener methods. This approach is more flexible and allows you to add event listeners on ordinary Spring bean methods.

import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.context.event.EventListener;

@Component
public class MyEventListener {
    
    

    @EventListener(ContextRefreshedEvent.class)
    public void onContextRefreshedEvent() {
    
    
        // 在这里执行初始化操作
    }
}

Personally, I prefer to use the annotation method in projects @PostConstruct; most usage scenarios involve preloading data into the cache.

Guess you like

Origin blog.csdn.net/weixin_45626288/article/details/132701239