springboot--启动服务自动加载额外配置实现

实现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("缓存数据库信息初始化成功!");
  }
}

猜你喜欢

转载自www.cnblogs.com/jvStarBlog/p/11136167.html