[Transfer] [SpringBoot] Briefly describe the three methods of springboot project startup data loading memory

I. Introduction

Generally speaking, the SpringBoot project environment configuration is placed in the properties file, and the configuration items of the properties/yaml file in the project are loaded into memory at startup. However, when the configuration items are changed in this way, it needs to be recompiled and deployed. Considering this factor, today I will introduce the configuration items stored in the database table, and the configuration items will be loaded into the 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 the memory when the project starts. The scenarios used are: loading configuration items into memory; loading dictionary or whitelist data into memory (or caching into Redis) at startup.

2. Loading method

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

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

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

Precautions

The official javadocs of the second and third types are the same, the difference is that the received parameters are different. The parameters of CommandLineRunner are the most primitive parameters without any processing. The parameter of ApplicationRunner is ApplicationArguments, which further encapsulates the original parameters.

3. Code example

3.1 Using the @PostConstruct annotation

package com.example.demo.config;
 
import com.example.demo.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Component
public class InitData1 {
 
    public static Map<Integer, String> codeMap = new HashMap<Integer, String>();
 
    @Autowired
    private ICodeService codeService;
 
    @PostConstruct
    public void init() {
        System.out.println("示例1:加载codeMap中......");
        // 查询数据库数据
        List<String> codeList = codeService.listAll();
        for (int i = 0; i < codeList.size(); i++) {
            codeMap.put(i, codeList.get(i));
        }
    }
 
    @PreDestroy
    public void destroy() {
        System.out.println("系统启动成功,codeMap加载完成!");
    }
}

3.2  CommandLineRunner interface

package com.example.demo.config;
 
import com.example.demo.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Component
@Order(1) // 初始化加载优先级,数字越小优先级越高
public 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 {
        System.out.println("示例2:加载codeMap中......");
        // 查询数据库数据
        List<String> codeList = codeService.listAll();
        for (int i = 0; i < codeList.size(); i++) {
            codeMap.put(i, codeList.get(i));
        }
    }
}

3.3  ApplicationRunner interface

package com.example.demo.config;
 
import com.example.demo.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Component
@Order(1) // 初始化加载优先级,数字越小优先级越高
public 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 {
        System.out.println("示例3:加载codeMap中......");
        // 查询数据库数据
        List<String> codeList = codeService.listAll();
        for (int i = 0; i < codeList.size(); i++) {
            codeMap.put(i, codeList.get(i));
        }
    }
}

Four. Summary

1. The timing of calling CommandLineRunner and ApplicationRunner is to call immediately after the initialization of the container is completed.

2. There is no difference in the use of CommandLineRunner and ApplicationRunner. The only difference is that CommandLineRunner accepts string array parameters and needs to parse out the key and value by itself. The parameter of ApplicationRunner is ApplicationArguments, which is a further package of the original parameters.

3. Both interfaces can use the @Order parameter, which supports the order of calls to be determined according to the weight value declared in order after the project starts (the smaller the number, the higher the priority).

end!

Related references:

SpringBoot--ApplicationRunner interface
—————————————————
Copyright statement: This article is the original article of CSDN blogger "No8g Siege Lion", following the CC 4.0 BY-SA copyright agreement, please reprint Attach the original source link and this statement.
Original link: https://blog.csdn.net/weixin_44299027/article/details/128973408

Guess you like

Origin blog.csdn.net/mao_mao37/article/details/129880612