SpringBoot构建的服务在启动完成时执行功能的三种方法

      当我们在用SpringBoot开发后端服务时,通常我们会有一些需求是需要在服务启动完成后提前运行的,比如:

  1. 将一些字典项数据从数据库加载到缓存,以方便在服务运行时快速从缓存获取。【调用@Autowired的Bean的方法从数据库获取需要缓存的数据】
  2. 要执行某些定时任务进行相关的统计与计算。【通过调用服务中带注解@Async的方法或用CompletableFuture类在方法中直接创建多线程实现】
  3. 启动心跳检测相关的线程。【同第2点】
  4. 检测是否某项配置已经开启,否则启动失败。【与@Value定义的属性或@ConfigurationProperties定义的类配合使用】
  5. 同时启动多个异步线程进行处理某些业务。【同第2点】

      要达到以上目的,在SpringBoot中有三种方法可以实现,如下所示:

  1. 实现ApplicationRunner接口【在所有的Bean加载到IOC容器之后,在打印出服务已经启动完成【Started DemoApplication in 1.464 seconds (JVM running for 1.744)】的日志时才会调用所有实现了此接口的Bean中的run方法】
  2. 实现CommandLineRunner接口 【同上,不过先会调用ApplicationRunner接口的实现类,其次再调用CommandLineRunner接口的实现类】
  3. 实现ApplicationListener接口 【在所有的Bean加载到IOC容器之后,所有的Runner方法也都运行过后,才会加载ApplicationListener接口的实现类,内部通过发送各种事件的机制进行执行相关的监听器,此处使用ApplicationReadyEvent事件来触发】

      SpringBoot启动的过程如下图所示【其中忽略一些细节,只列出主要的几个步聚】:

其中橘黄色部分代表springboot会在适当的时机调用上面三种实现。

上面三种方法实现示例如下所示:

1. 实现ApplicationRunner接口

package com.crh.demo.listener;

import com.crh.demo.service.cache.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * This runner is used to execute the pre-load functions of the cache service, schedule service,
 * heart-check service before all beans has been loaded to Application Context.
 *
 * @author crh
 */
@Slf4j
@Component
@Order(0)
public class PrepareProgressApplicationRunner implements ApplicationRunner {

    @Autowired
    private CacheService cacheService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("[PPAR]-[run]-[INF]-[IC0001]:[加载所有的缓存数据到内存中.]");
        cacheService.loadAllCachedData();
    }

}

2. 实现CommandLineRunner接口

package com.crh.demo.listener;

import com.crh.demo.service.cache.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * This runner is used to execute the pre-load functions of the cache service, schedule service,
 * heart-check service before all beans has been loaded to Application Context.
 *
 * @author crh
 */
@Slf4j
@Component
@Order(1)
public class PrepareProgressCommandRunner implements CommandLineRunner {

    @Autowired
    private CacheService cacheService;

    @Override
    public void run(String... args) throws Exception {
        log.info("[PPCR]-[run]-[INF]-[IC0001]:[加载所有的缓存数据到内存中.]");
        cacheService.loadAllCachedData();
    }

}

3. 实现ApplicationListener接口

package com.crh.demo.listener;

import com.crh.demo.service.cache.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * This listener is used to start the listening functions of the cache service, schedule service,
 * heart-check service before all beans has been loaded to Application Context.
 *
 * @author crh
 */
@Slf4j
@Component
@Order(0)
public class PrepareProcessListener implements ApplicationListener<ApplicationReadyEvent>, Ordered{

    @Autowired
    private CacheService cacheService;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
//        throw new RuntimeException("test"); 此处主要是为在启动执行预加载服务时出错阻止服务启动成功。
        log.info("[PPL]-[onApplicationEvent]-[INF]-[IC0001]:[加载所有的缓存数据到内存中.]");
        cacheService.loadAllCachedData();
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}

以上三种方法的运行结果如下:

2021-04-07 01:01:56.936  INFO 11940 --- [           main] com.crh.demo.DemoApplication             : Started DemoApplication in 1.492 seconds (JVM running for 1.784)
2021-04-07 01:01:56.937  INFO 11940 --- [           main] c.c.d.l.PrepareProgressApplicationRunner : [PPAR]-[run]-[INF]-[IC0001]:[加载所有的缓存数据到内存中.]
2021-04-07 01:01:56.937  INFO 11940 --- [           main] c.c.d.l.PrepareProgressCommandRunner     : [PPCR]-[run]-[INF]-[IC0001]:[加载所有的缓存数据到内存中.]
2021-04-07 01:01:56.937  INFO 11940 --- [           main] c.c.d.listener.PrepareProcessListener    : [PPL]-[onApplicationEvent]-[INF]-[IC0001]:[加载所有的缓存数据到内存中.]

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
# 前端自动化测试框架(UI Autotest Framework) 框架提供统一的接口、设计原语和开发模式,支持 APP、微信、HTML5、Web 网页。自动化测试用例开发人员只需要学习一次,就可以编写前端自动化测试用例,对前端产品进行自动化测试。 ## 前端自动化测试框架包含如下模块: APP 自动化测试框架、HTML5 网页自动化测试框架、Web网页自动化测试框架。 * APP 自动化测试框架主要用于移动端APP自动化测试项目,目标程序运行在移动设备上。 * HTML5 网页自动化测试框架主要用于移动端H5网页(比如微信程序),目标网页通过移动设备上的 Chrome 或者 Safari 浏览器运行。 * Web 网页自动化测试主要用于 PC 端网页,目前支持 Chrome 、Safari 、Firefox 、IE 、Edge 浏览器。 ## 特性 Autotest Framework 有如下特性: * 采用Java语言,基于 SpringBoot 框架。 * 基于 Page Object 设计模式,将 UI 界面抽象为 Page Object,可以减少重复代码和降低维护成本。 * 基于 TesgNG 测试框架构建测试用例,支持钉钉消息通知、失败截屏、HTTP 报告、并发执行等特性。 * 统一管理和维护 Adb 连接、Appium server,对上层测试程序屏蔽实现细节,降低测试人员编写用例难度。 * 封装和抽象配置和数据仓库,直接注入到测试用例中,无需额外获取。 ## 架构 ### APP 测试框架的逻辑视图 ![APP 测试框架逻辑视图](https://s2.ax1x.com/2019/09/11/nwC234.jpg) 测试程序主要分为三层: * APP 自动化测试程序层,包含 Page Object 对象和测试用例 * APP Framework 层,主要提供统一的系统封装 * Appium Server Manger 层,提供 Adb 连接、Appium Server、Apk 的管理和维护 ### APP 测试框架模块视图 ![APP 测试框架模块视图](https://s2.ax1x.com/2019/09/11/nwCWv9.jpg) ## 开发指南 ### 1. 创建测试项目 以 APP 自动化测试为例:只需要创建一个自动化测试项目,并且依赖 APP 自动化测试框架 autotest-app 即可。 ```xml <dependencies> <dependency> <groupId>com.qianmi</groupId> <artifactId>autotest-app</artifactId> <version>2.0.0-SNAPSHOT</version> </dependency> </dependencies> ``` 再配置一个 SpringBoot 的 Maven 打包插件,mainClass 属性配置为对应框架的启动类。 * APP 的启动类为:**`com.qianmi.autotest.app.AppTestApplication`** * HTML5 的启动类为:**`com.qianmi.autotest.html5.Html5TestApplication`** * Web 的启动类为:**`com.qianmi.autotest.web.WebTestApplication`** ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值