资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

springboot项目启动时初始化资源如何解决-创新互联

springboot项目启动时初始化资源如何解决?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

成都创新互联公司专注于企业全网营销推广、网站重做改版、金溪网站定制设计、自适应品牌网站建设、H5网站设计商城网站制作、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为金溪等各大城市提供网站开发制作服务。

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

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

@Component
public class Runner implements CommandLineRunner {
  
  @Override
  public void run(String... args) throws Exception {
    System.out.println("The Runner start to initialize ...");
  }
  
}

启动项目进行测试:

...
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 的实现类来进行测试:

第一个实现类:

@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.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()里面的值越小启动越早。

看完上述内容,你们掌握springboot项目启动时初始化资源如何解决的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


文章标题:springboot项目启动时初始化资源如何解决-创新互联
链接分享:http://cdkjz.cn/article/docgsd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220