Springboot项目启动自动执行方法
补充知识点:
Spring Boot如何解决项目启动时初始化资源,在我们实际工作中,总会遇到这样需求,在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等。
为了达到这个目的,我们需要使用CommandLineRunner或ApplicationRunner接口创建bean,spring boot会自动监测到它们。这两个接口都有一个run()方法,在实现接口时需要覆盖该方法,并使用@Component注解使其成为bean。
CommandLineRunner和ApplicationRunner的作用是相同的。不同之处在于CommandLineRunner接口的run()方法接收String数组作为参数,即是最原始的参数,没有做任何处理;而ApplicationRunner接口的run()方法接收ApplicationArguments对象作为参数,是对原始参数做了进一步的封装。
当程序启动时,我们传给main()方法的参数可以被实现CommandLineRunner和ApplicationRunner接口的类的run()方法访问,即可接收启动服务时传过来的参数。我们可以创建多个实现CommandLineRunner和ApplicationRunner接口的类。为了使他们按一定顺序执行,可以使用@Order注解或实现Ordered接口。(添加 @Order
注解的实现类最先执行,并且@Order()
里面的值越小启动越早。)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
方法一:
平时项目里可能会遇到,在启动项目的时候,需要进行初始化操作,如执行一段SQL脚本,或者提前对一些类进行实例化。这时候可以使用ApplicationRunner接口进行操作。下面已我自己项目为例,记录在项目中应用(实现)ApplicationRunner的方法。
在某个包下创建一个实体类,该类需要继承ApplicationRunner接口并实现里面的run方法,并将该类通过@Component 注入到spring里,如下所示:
@Component
public class InitProject implements ApplicationRunner {
@Resource
ParkService parkService;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("===================做自己最害怕的事,会使你进步更大!======================");
// List parkList = parkService.getParkList();
// for (Park p:parkList) {
// System.out.println("测试随项目启动:" + p.getName());
// }
}
}
这时候,当我们启动项目的时候,就会执行run内日志打印的方法,控制台打印效果如下;
019-12-26 17:49:04.408 INFO 32649 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8088 (http) with context path ''
2019-12-26 17:49:04.410 INFO 32649 --- [ restartedMain] com.ow.dva.DvaApplication : Started DvaApplication in 5.107 seconds (JVM running for 5.622)
===================做自己最害怕的事,会使你进步更大!======================
方法二:
在项目启动完成以后,自动执行的方法,(针对需要注入别的javaBean的时候使用),如下如下所示:
@SpringBootApplication
public class DvaApplication {
//static Logger logger = LoggerFactory.getLogger("Dva");
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(DvaApplication.class, args);
//项目启动后执行
ParkService parkService = run.getBean(ParkService.class);
System.out.println("==================================>>" + parkService.getParkList().size());
}
}
这时候,当我们项目后就会执行,控制台打印效果如下;
2019-12-26 17:49:04.408 INFO 32649 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8088 (http) with context path ''
2019-12-26 17:49:04.410 INFO 32649 --- [ restartedMain] com.ow.dva.DvaApplication : Started DvaApplication in 5.107 seconds (JVM running for 5.622)
===================做自己最害怕的事,会使你进步更大!======================
===============收到心跳包===================
===============收到心跳包===================
===============收到心跳包===================
2019-12-26 17:49:04.481 DEBUG 32649 --- [ restartedMain] c.o.d.m.p.mapper.ParkMapper.getParkList : ==> Preparing: select id, number, name, park_type, province, city, county, address, longitude, latitude, open_status, fee_status, over_parkinglot_status, img_url, parkinglot_total, parkinglot_surplus, open_start_time, open_stop_time, intro, create_time from dva_park where 1 = 1 order by create_time desc
2019-12-26 17:49:04.510 DEBUG 32649 --- [ restartedMain] c.o.d.m.p.mapper.ParkMapper.getParkList : ==> Parameters:
2019-12-26 17:49:04.523 DEBUG 32649 --- [ restartedMain] c.o.d.m.p.mapper.ParkMapper.getParkList : <== Total: 2
===============项目启动后执行===================>>2
2019-12-26 17:49:04.736 INFO 32649 --- [on(5)-127.0.0.1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-12-26 17:49:04.736 INFO 32649 --- [on(5)-127.0.0.1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-12-26 17:49:04.746 INFO 32649 --- [on(5)-127.0.0.1] o.s.web.servlet.DispatcherServlet : Completed initialization in 9 ms
2019-12-26 17:49:04.827 INFO 32649 --- [on(3)-127.0.0.1] io.lettuce.core.EpollProvider : Starting without optional epoll library
2019-12-26 17:49:04.828 INFO 32649 --- [on(3)-127.0.0.1] io.lettuce.core.KqueueProvider : Starting without optional kqueue library