实现CommandLineRunner 接口,springboot在启动时会自动调用run方法。通过@Order注解可以指定执行顺序。
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Component @Order(1) public class InitExtraResource implements CommandLineRunner { @Autowired MetadataDubboService metadataDubboService; public static final Logger logger = LoggerFactory.getLogger(InitExtraResource.class); @Override public void run(String... strings) { logger.info("缓存数据库信息初始化开始。。。"); metadataDubboService.refreshCache("ALL");//多线程实现,否则阻塞主线程 logger.info("缓存数据库信息初始化成功!"); } }
原文地址:https://www.cnblogs.com/jvStarBlog/p/11136167.html
时间: 2024-10-29 19:10:43