当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Spring Boot中初始化资源的几种方式

详解Spring Boot中初始化资源的几种方式

2019年07月19日  | 移动技术网IT编程  | 我要评论

室内设计. 闰研喜,造梦西游4试玩版,本溪虎跃快客时刻表

假设有这么一个需求,要求在项目启动过程中,完成线程池的初始化,加密证书加载等功能,你会怎么做?如果没想好答案,请接着往下看。今天介绍几种在spring boot中进行资源初始化的方式,帮助大家解决和回答这个问题。

commandlinerunner

  1. 定义初始化类 mycommandlinerunner
  2. 实现 commandlinerunner 接口,并实现它的 run() 方法,在该方法中编写初始化逻辑
  3. 注册成bean,添加 @component注解即可

示例代码如下:

@component
public class mycommandlinerunner implements commandlinerunner {
 
  @override
  public void run(string... args) throws exception {
    system.out.println("...init resources by implements commandlinerunner");
  }
  
}

实现了 commandlinerunner 接口的 component 会在所有 spring beans 初始化完成之后, 在 springapplication.run() 执行之前完成。下面通过加两行打印来验证我们的测试。

@springbootapplication
public class demoapplication {
 
  public static void main(string[] args) {
    system.out.println("... start springapplication.run()");
    springapplication.run(demoapplication.class, args);
    system.out.println("... end springapplication.run()");
  }
  
}

控制台打印结果如下。

... start springapplication.run()
 
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: spring boot ::       (v1.5.11.release)
。。。。。。(此处省略一堆打印信息)
2018-05-02 17:01:19.700  info 21236 --- [           main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat started on port(s): 8080 (http)
...init resources by implements commandlinerunner
2018-05-02 17:01:19.708  info 21236 --- [           main] cn.mariojd.demo.demoapplication          : started demoapplication in 2.282 seconds (jvm running for 3.125)
... end springapplication.run()

applicationrunner

  1. 定义初始化类 myapplicationrunner
  2. 实现 applicationrunner 接口,并实现它的 run() 方法,在该方法中编写初始化逻辑
  3. 注册成bean,添加 @component注解即可

示例代码如下:

@component
public class myapplicationrunner implements applicationrunner {
 
  @override
  public void run(applicationarguments applicationarguments) throws exception {
    system.out.println("...init resources by implements applicationrunner");
  }
 
}

可以看到,通过实现 applicationrunner 接口,和通过实现 commandlinerunner 接口都可以完成项目的初始化操作,实现相同的效果。两者之间唯一的区别是 run() 方法中自带的形参不相同,在 commandlinerunner 中只是简单的string... args形参,而 applicationrunner 则是包含了 applicationarguments 对象,可以帮助获得更丰富的项目信息。

applicationarguments

@order

如果项目中既有实现了 applicationrunner 接口的初始化类,又有实现了 commandlinerunner 接口的初始化类,那么会是哪一个先执行呢?测试告诉我们,答案是实现了 applicationrunner 接口的初始化类先执行,我想这点倒是不需要大家过分去关注为什么。但如果需要改变两个初始化类之间的默认执行顺序,那么使用 @order 注解就可以帮助我们解决这个问题。

@order
@component
@order(1)
public class mycommandlinerunner implements commandlinerunner {
 
  @override
  public void run(string... args) throws exception {
    system.out.println("...init resources by implements commandlinerunner");
  }
}
@component
@order(2)
public class myapplicationrunner implements applicationrunner {
 
  @override
  public void run(applicationarguments applicationarguments) throws exception {
    system.out.println("...init resources by implements applicationrunner");
  }
 
}

最终,控制台中打印如下。通过控制台输出我们发现, @order 注解值越小,该初始化类也就越早执行。

。。。。。。(此处省略一堆打印信息)
2018-05-02 17:27:31.450  info 28304 --- [           main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat started on port(s): 8080 (http)
...init resources by implements commandlinerunner
...init resources by implements applicationrunner
2018-05-02 17:27:31.453  info 28304 --- [           main] cn.mariojd.demo.demoapplication          : started demoapplication in 2.086 seconds (jvm running for 2.977)

@postconstruct

使用 @postconstruct 注解同样可以帮助我们完成资源的初始化操作,前提是这些初始化操作不需要依赖于其它spring beans的初始化工作。

@postconstruct

可以看到 @postconstruct 注解是用在方法上的,写一个方法测试一下吧。

  @postconstruct
  public void postconstruct() {
    system.out.println("... postconstruct");
  }

启动项目,控制台中最终打印如下。

... start springapplication.run()
 
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: spring boot ::       (v1.5.11.release)
。。。。。。(此处省略一堆打印信息)
... postconstruct
。。。。。。(此处省略一堆打印信息)
2018-05-02 17:40:22.300  info 29796 --- [           main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat started on port(s): 8080 (http)
...init resources by implements commandlinerunner
...init resources by implements applicationrunner
2018-05-02 17:40:22.303  info 29796 --- [           main] cn.mariojd.demo.demoapplication          : started demoapplication in 2.387 seconds (jvm running for 3.267)
... end springapplication.run()

文末小结

综上,使用 @postconstruct 注解进行初始化操作的顺序是最快的,前提是这些操作不能依赖于其它bean的初始化完成。通过添加 @order 注解,我们可以改变同层级之间不同bean的加载顺序。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网