Springboot啓動時加載數據到內存中

前言

  一般來說,springboot工程環境配置放在properties文件中,啓動的時候將工程中的properties/yaml文件的配置項加載到內存中。但這種方式改配置項的時候,需要重新編譯部署,考慮到這種因素,今天介紹將配置項存到數據庫表中,在工程啓動時把配置項加載到內存中。
  springboot提供了兩個接口:CommandLineRunner和ApplicationRunner。實現其中接口,就可以在工程啓動時將數據庫中的數據加載到內存。實現任意接口可實現以下場景:加載配置項到內存中;啓動時將字典或白名單數據加載到內存(或緩存到redis中)。

一、CommandLineRunner實現

1、CommandLineRunner接口

1) 接口詳解

在這裏插入圖片描述
  官方doc:

Interface used to indicate that a bean should run when it is contained within a SpringApplication.
Multiple CommandLineRunner beans can be defined within the same application context and can be orderedusing the Ordered interface or Order @Order annotation.

  這裏是引用接口被用作將其加入spring容器中時執行其run方法。多個CommandLineRunner可以被同時執行在同一個spring上下文中並且執行順序是以order註解的參數順序一致。

If you need access to ApplicationArguments instead of the raw String arrayconsider using ApplicationRunner.

  如果你需要訪問ApplicationArguments去替換掉字符串數組,可以考慮使用ApplicationRunner類。

2)代碼演示

  在我們新建好工程後,爲了簡單我們直接使用Application類實現CommandLineRunner接口(或者新建類實現並使用@Component註解),這個類的註解@SpringBootApplication會爲我們自動配置。

package com.skh.springboot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SpringbootApplication implements CommandLineRunner {
    private static Logger logger = LoggerFactory.getLogger(SpringbootApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        logger.info("服務已啓動,執行command line runner。");

        for (int i = 0; i < args.length; ++i) {
            logger.info("args[{}]: {}", i, args[i]);
        }
    }
}


  啓動服務,控制檯查看日誌如下,發現run()方法被正常地執行了:
在這裏插入圖片描述
  run()方法有個可變參數args,這個參數是用來接收命令行參數的,我們下面來加入參數來測試一下:
在這裏插入圖片描述
  重啓,查看控制檯日誌,發現傳入的參數已經打印出來:
在這裏插入圖片描述

2、ApplicationRunner接口

1)接口詳解

在這裏插入圖片描述
  二者的官方javadoc一樣,區別在於接收的參數不一樣。CommandLineRunner的參數是最原始的參數,沒有做任何處理。ApplicationRunner的參數是ApplicationArguments,是對原始參數做了進一步的封裝。
  ApplicationArguments是對參數(main方法)做了進一步的處理,可以解析–name=value的,我們就可以通過name來獲取value(而CommandLineRunner只是獲取–name=value)。

在這裏插入圖片描述
可以接收–foo=bar這樣的參數。
–getOptionNames()方法可以得到foo這樣的key的集合。
–getOptionValues(String name)方法可以得到bar這樣的集合的value。

2)代碼演示

定義MyApplicationRunner類繼承ApplicationRunner接口。

package com.skh.springboot;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("===MyApplicationRunner==="+ Arrays.asList(args.getSourceArgs()));
        System.out.println("===getOptionNames========"+args.getOptionNames());
        System.out.println("===getOptionValues======="+args.getOptionValues("param"));
        System.out.println("==getOptionValues========"+args.getOptionValues("developer.name"));
    }
}

然後加入啓動參數
在這裏插入圖片描述
控制檯查看日誌打印
在這裏插入圖片描述

總結

1、CommandLineRunner和ApplicationRunner調用的時機是在容器初始化完成之後,立即調用。
2、CommandLineRunner和ApplicationRunner使用上沒有區別,唯一區別是CommandLineRunner接受字符串數組參數,需要自行解析出健和值,ApplicationRunner的參數是ApplicationArguments,是對原始參數做了進一步的封裝。
3、兩個接口都可以使用@Order參數,支持工程啓動後根據order聲明的權重值來覺得調用的順序(數字越小,優先級越高)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章