项目过程中会遇到需要做一些初始化操作,如线程池初始化、加载初始化参数等,同时可能需要有不同的加载顺序的要求。今天介绍在springboot工程下如何解决项目启动时初始化资源的问题。
启动类:
/** * * @version 1.0 * @description: 启动类 * @date 2020-09-14 19:41 */ @SpringBootApplication public class CommandLineRunnerApplication { public static void main(String[] args) { System.out.println("The service to start"); SpringApplication.run(CommandLineRunnerApplication.class, args); System.out.println("The service has started"); } }
项目启动初始化参数,可实现CommandLineRunner
接口类,实现 run() 方法。
/** * * @version 1.0 * @description: 实现CommandLineRunner接口的初始化类 * @date 2020-09-14 19:43 */ @Component public class Runner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The Runner start to initialize"); } }
运行项目的启动类,运行结果如下:
下面实现按照顺序类初始化参数的方法,通过 @Order 注解来控制执行顺序。
/** * * @version 1.0 * @description: 第一个启动初始化参数类 * @date 2020-09-14 19:44 */ @Component @Order(1) public class FirstRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The FirstRunner start to initialize"); } }
/** * @version 1.0 * @description: 第二个启动初始化参数类 * @date 2020-09-14 19:44 */ @Component @Order(2) public class SecondRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The SecondRunner start to initialize"); } }
启动工程的启动类,执行结果顺序如下图:
The service to start . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.7.RELEASE) 2020-09-14 20:09:52.427 INFO 9184 --- [ main] t.z.blog.CommandLineRunnerApplication : Starting CommandLineRunnerApplication on zhangliang with PID 9184 (E:\weixinworkspace\springboot-parent\springboot-init\target\classes started by Administrator in E:\weixinworkspace\springboot-parent) 2020-09-14 20:09:52.430 INFO 9184 --- [ main] t.z.blog.CommandLineRunnerApplication : No active profile set, falling back to default profiles: default 2020-09-14 20:09:53.420 ERROR 9184 --- [ main] o.a.catalina.core.AprLifecycleListener : An incompatible version [1.1.27] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14] 2020-09-14 20:09:53.719 ERROR 9184 --- [ main] o.a.catalina.core.AprLifecycleListener : An incompatible version [1.1.27] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14] 2020-09-14 20:09:53.808 INFO 9184 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-09-14 20:09:53.826 ERROR 9184 --- [ main] o.a.catalina.core.AprLifecycleListener : An incompatible version [1.1.27] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14] 2020-09-14 20:09:53.841 INFO 9184 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-09-14 20:09:53.841 INFO 9184 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22] 2020-09-14 20:09:53.950 INFO 9184 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-09-14 20:09:53.950 INFO 9184 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1451 ms 2020-09-14 20:09:54.162 INFO 9184 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-09-14 20:09:54.383 INFO 9184 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-09-14 20:09:54.387 INFO 9184 --- [ main] t.z.blog.CommandLineRunnerApplication : Started CommandLineRunnerApplication in 2.303 seconds (JVM running for 2.666) The FirstRunner start to initialize The SecondRunner start to initialize The Runner start to initialize The service has started
实现 CommandLineRunner 接口中的 run()
方法,同时 @Order
注解的实现类最先执行,并且@Order()
里面的值越小启动越早。