springboot项目启动成功后执行一段代码的两种方式

实现ApplicationRunner接口

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;


@Component
public class DemoApplicationRunner implements ApplicationRunner {


Logger log = LoggerFactory.getLogger(DemoApplicationRunner.class);


@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
log.info("项目启动完成!!!!!!!");
System.out.println("ApplicationRunner");
}
}

 

实现CommandLineRunner接口


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;


@Component
public class DemoApplicationRunner implements CommandLineRunner {


Logger log = LoggerFactory.getLogger(DemoApplicationRunner.class);


@Override
public void run(String... strings) throws Exception {
    log.info("项目启动完成!!!!!!!");
   System.out.println("ApplicationRunner");
}


}

 
原文地址:https://www.cnblogs.com/nongzihong/p/15398681.html