SoFunction
Updated on 2025-03-08

Three ways to start data load memory in SpringBoot project

1. Preface

Generally speaking, SpringBoot project environment configuration is placed in properties file. When starting, load the configuration items of properties/yaml files in the project into memory. However, when changing configuration items in this way, it is necessary to recompile and deploy. Considering this factor, today we will introduce to store the configuration items in the database table and load the configuration items into memory when the project starts.

SpringBoot provides two interfaces: CommandLineRunner and ApplicationRunner . By implementing the interface, the data in the database can be loaded into memory when the project starts. The scenarios used are: loading configuration items into memory; loading dictionary or whitelist data into memory (or cached into Redis) at startup.

2. Loading method

The first one: use @PostConstruct annotation (properties/yaml file).

The second type: use the @Order annotation and the CommandLineRunner interface.

The third type: use @Order annotation and ApplicationRunner interface.

Things to note

The second and third types are the same as the official javadoc of the two, the difference is that the received parameters are different. The parameters of CommandLineRunner are the most original parameters and have no processing done. The parameter of ApplicationRunner is ApplicationArguments, which further encapsulates the original parameters.

3. Code examples

3.1 Annotation using @PostConstruct

package ;
 
import ;
import ;
import ;
 
import ;
import ;
import ;
import ;
import ;
 
@Component
public class InitData1 {
 
    public static Map<Integer, String> codeMap = new HashMap<Integer, String>();
 
    @Autowired
    private ICodeService codeService;
 
    @PostConstruct
    public void init() {
        ("Example 1: Loading codeMap...");
        // Query database data        List<String> codeList = ();
        for (int i = 0; i < (); i++) {
            (i, (i));
        }
    }
 
    @PreDestroy
    public void destroy() {
        ("The system started successfully, codeMap loaded!");
    }
}

3.2 CommandLineRunner interface

package ;
 
import ;
import ;
import ;
import ;
import ;
 
import ;
import ;
import ;
 
@Component
@Order(1) // Initialize the load priority, the smaller the number, the higher the prioritypublic class InitData2 implements CommandLineRunner {
 
    public static Map<Integer, String> codeMap = new HashMap<Integer, String>();
 
    @Autowired
    private ICodeService codeService;
 
    @Override
    public void run(String... args) throws Exception {
        ("Example 2: Loading codeMap...");
        // Query database data        List<String> codeList = ();
        for (int i = 0; i < (); i++) {
            (i, (i));
        }
    }
}

3.3 ApplicationRunner interface

package ;
 
import ;
import ;
import ;
import ;
import ;
import ;
 
import ;
import ;
import ;
 
@Component
@Order(1) // Initialize the load priority, the smaller the number, the higher the prioritypublic class InitData3 implements ApplicationRunner {
 
    public static Map<Integer, String> codeMap = new HashMap<Integer, String>();
 
    @Autowired
    private ICodeService codeService;
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        ("Example 3: Loading codeMap...");
        // Query database data        List<String> codeList = ();
        for (int i = 0; i < (); i++) {
            (i, (i));
        }
    }
}

4. Summary

1. The time when CommandLineRunner and ApplicationRunner are called is called immediately after the container initialization is completed.

2. There is no difference between CommandLineRunner and ApplicationRunner. The only difference is that CommandLineRunner accepts string array parameters and needs to parse the strength and value by itself. The parameter of ApplicationRunner is ApplicationArguments, which further encapsulates the original parameters.

3. Both interfaces can use the @Order parameter, which supports the order of calls to be determined based on the weight value declared by the order after the project is started (the smaller the number, the higher the priority).

The above is the detailed content of three methods in the SpringBoot project to start data loading memory. For more information about SpringBoot data loading memory, please pay attention to my other related articles!