标签:CommandLineRunner 20 springboot 18 09 9184 2020 main 14
项目过程中会遇到需要做一些初始化操作,如线程池初始化、加载初始化参数等,同时可能需要有不同的加载顺序的要求。今天介绍在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()
里面的值越小启动越早。
标签:CommandLineRunner,20,springboot,18,09,9184,2020,main,14 来源: https://www.cnblogs.com/yb-ken/p/15799439.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。