好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

springboot使用CommandLineRunner解决项目启动时初始化资源的操作

前言:

 

在我们实际工作中,总会遇到这样需求,在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等。

今天就给大家介绍一个 spring boot 神器,专门帮助大家解决项目启动初始化资源操作。

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

正文:

 

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

?

1

2

3

4

5

6

7

8

9

10

@springbootapplication

public class springbootrabbitmqapplication {

 

     public static void main(string[] args) {

     system.out.println( "the service to start" );

       springapplication.run(springbootrabbitmqapplication. class , args);

     system.out.println( "the service to started" );

     }

 

}

接下来我们直接创建一个类继承 commandlinerunner ,并实现它的 run() 方法。

?

1

2

3

4

5

6

7

8

9

@component

public class runner implements commandlinerunner {

  

   @override

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

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

   }

  

}

启动项目进行测试:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

...

the service to start.

 

  .  ____     _      __ _ _

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

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

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

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

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

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

 

...

2021 - 02 - 01 11 : 38 : 31.314 [main] info o.s.boot.web.embedded.tomcat.tomcatwebserver - tomcat started on port(s): 8078 (http) with context path ''

2021 - 02 - 01 11 : 38 : 31.317 [main] info com.cn.springbootrabbitmqapplication - started springbootrabbitmqapplication in 4.124 seconds (jvm running for 6.226 )

the runner start to initialize ...

the service to started

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

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

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

第一个实现类:

?

1

2

3

4

5

6

7

8

@component

@order ( 1 )

public class orderrunner1 implements commandlinerunner {

   @override

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

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

   }

}

第二个实现类:

?

1

2

3

4

5

6

7

8

@component

@order ( 2 )

public class orderrunner2 implements commandlinerunner {

   @override

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

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

   }

}

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

...

the service to start.

  .  ____     _      __ _ _

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

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

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

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

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

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

 

...

2021 - 02 - 01 11 : 42 : 05.724 [main] info o.s.boot.web.embedded.tomcat.tomcatwebserver - tomcat started on port(s): 8078 (http) with context path ''

2021 - 02 - 01 11 : 42 : 05.728 [main] info com.cn.springbootrabbitmqapplication - started springbootrabbitmqapplication in 3.472 seconds (jvm running for 5.473 )

the orderrunner1 start to initialize ...

the orderrunner2 start to initialize ...

the runner start to initialize ...

the service to started

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

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

以上就是springboot使用commandlinerunner解决项目启动时初始化资源的操作的详细内容,更多关于springboot 解决项目启动时初始化资源的操作的资料请关注其它相关文章!

原文链接:https://juejin.cn/post/6924146777675268109

查看更多关于springboot使用CommandLineRunner解决项目启动时初始化资源的操作的详细内容...

  阅读:17次