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");
}


}

 

 

posted @ 2021-10-12 17:06  随★风  阅读(344)  评论(0编辑  收藏  举报