新建一个类继承CommandLineRunner类,实现其下的run方法,方法体中写自己想初始化的代码即可:
package com.han.shopping.handler;
import com.han.shopping.service.InitialRedisService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class InitialExecute implements CommandLineRunner {
@Resource
private InitialRedisService initialRedisService;
@Override
public void run(String... args) throws Exception {
initialRedisService.initial();
}
}