java项目启动时初始化_Spring Boot解决项目启动时初始化资源的方法

本文介绍了一个Spring Boot神器CommandLineRunner,用于项目启动时执行初始化操作。CommandLineRunner接口的组件会在所有Spring Beans初始化后执行,适合进行数据初始化。文章通过示例展示了如何使用该接口并确保多个初始化任务按指定顺序执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

在我们实际工作中,总会遇到这样需求,在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等。今天就给大家介绍一个 spring boot 神器,专门帮助大家解决项目启动初始化资源操作。

这个神器就是 commandlinerunner, commandlinerunner 接口的 component 会在所有 springbeans都初始化之后, springapplication.run()之前执行,非常适合在应用程序启动之初进行一些数据初始化的工作。

接下来我们就运用案例测试它如何使用,在测试之前在启动类加两行打印提示,方便我们识别 commandlinerunner 的执行时机。

@springbootapplicationpublic 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() 方法。

@component

public class runner implements commandlinerunner {

@override

public void run(string... args) throws exception {

system.out.println("the runner start to initialize ...");

}

}

我们在 run() 方法中打印了一些参数来看出它的执行时机。完成之后启动项目进行测试:

...

the service to start.

____ __ _ _ /\\ / ___'_ __ _ _(_)_ __

__ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

\\/ ___)| |_)| | | | | || (_| | ) ) ) )

' |____| .__|_| |_|_| |_\__, | / / / /

=========|_|==============|___/=/_/_/_/

:: spring boot :: (v2.0.0.release)

...

2018-04-21 22:21:34.706 info 27016 --- [ main] o.s.b.w.embedded.tomcat.tomcatwebserver : tomcat started on port(s): 8080 (http) with context path ''

2018-04-21 22:21:34.710 info 27016 --- [ main] com.neo.commandlinerunnerapplication : started commandlinerunnerapplication in 3.796 seconds (jvm running for 5.128)

the runner start to initialize ...

the service has started.

根据控制台的打印信息我们可以看出 commandlinerunner 中的方法会在 spring boot 容器加载之后执行,执行完成后项目启动完成。

如果我们在启动容器的时候需要初始化很多资源,并且初始化资源相互之间有序,那如何保证不同的 commandlinerunner 的执行顺序呢?spring boot 也给出了解决方案。那就是使用 @order 注解。

我们创建两个 commandlinerunner 的实现类来进行测试:

第一个实现类:

@component

@order(1)

public class orderrunner1 implements commandlinerunner {

@override

public void run(string... args) throws exception {

system.out.println("the orderrunner1 start to initialize ...");

}

}

第二个实现类:

@component

@order(2)

public class orderrunner2 implements commandlinerunner {

@override

public void run(string... args) throws exception {

system.out.println("the orderrunner2 start to initialize ...");

}

}

添加完成之后重新启动,观察执行顺序:

...

the service to start

. ____ _ __ _ _

/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

\\/ ___)| |_)| | | | | || (_| | ) ) ) )

' |____| .__|_| |_|_| |_\__, | / / / /

=========|_|==============|___/=/_/_/_/

:: spring boot :: (v2.0.0.release)

...

2018-04-21 22:21:34.706 info 27016 --- [ main] o.s.b.w.embedded.tomcat.tomcatwebserver : tomcat started on port(s): 8080 (http) with context path ''

2018-04-21 22:21:34.710 info 27016 --- [ main] com.neo.commandlinerunnerapplication : started commandlinerunnerapplication in 3.796 seconds (jvm running for 5.128)

the orderrunner1 start to initialize ...

the orderrunner2 start to initialize ...

the runner start to initialize ...

the service has started.

通过控制台的输出我们发现,添加 @order 注解的实现类最先执行,并且 @order()里面的值越小启动越早。

在实践中,使用 applicationrunner也可以达到相同的目的,两着差别不大。

示例代码: ()

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对萬仟网的支持。

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值