温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Boot中如何初始化资源

发布时间:2021-06-11 14:41:56 来源:亿速云 阅读:155 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关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()

看完上述内容,你们对Spring Boot中如何初始化资源有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI