有时候我们需要在spring boot容器启动并加载完后,开一些线程或者一些程序来干某些事情。这时候我们需要配置ContextRefreshedEvent事件来实现我们要做的事情

1、ApplicationStartup类

public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent>{
public void onApplicationEvent(ContextRefreshedEvent event)
{
//在容器加载完毕后获取dao层来操作数据库
OSSVideoRepository ossVideoRepository = (OSSVideoRepository)event.getApplicationContext().getBean(OSSVideoRepository.class);
//在容器加载完毕后获取配置文件中的配置
ServerConfig serverConfig = (ServerConfig)event.getApplicationContext().getBean(ServerConfig.class); ServerFileScanner fileScanner = new ServerFileScanner(
ossVideoRepository, serverConfig.getScanpath());
//在容器加载完毕后启动线程
Thread thread = new Thread(fileScanner);
thread.start();
}
}

2、ServerConfig 类

@Component
@ConfigurationProperties(prefix = "server")
public class ServerConfig {
private String aliyunossEndpoint;
private String aliyunossAccessKeyId;
private String aliyunossAccessKeySecret;
private String aliyunossBucketName;
private String scanpath; public String getAliyunossEndpoint() {
return aliyunossEndpoint;
} public void setAliyunossEndpoint(String aliyunossEndpoint) {
this.aliyunossEndpoint = aliyunossEndpoint;
} public String getAliyunossAccessKeyId() {
return aliyunossAccessKeyId;
} public void setAliyunossAccessKeyId(String aliyunossAccessKeyId) {
this.aliyunossAccessKeyId = aliyunossAccessKeyId;
} public String getAliyunossAccessKeySecret() {
return aliyunossAccessKeySecret;
} public void setAliyunossAccessKeySecret(String aliyunossAccessKeySecret) {
this.aliyunossAccessKeySecret = aliyunossAccessKeySecret;
} public String getAliyunossBucketName() {
return aliyunossBucketName;
} public void setAliyunossBucketName(String aliyunossBucketName) {
this.aliyunossBucketName = aliyunossBucketName;
} public String getScanpath() {
return scanpath;
} public void setScanpath(String scanpath) {
this.scanpath = scanpath;
} }

PS:还有一些spring内置的事件

1、 ContextRefreshedEvent:ApplicationContext容器初始化或者刷新时触发该事件。
2、 ContextStartedEvent:当使用ConfigurableApplicationContext接口的start()方法启动ApplicationContext容器时触发该事件。
3、 ContextClosedEvent:当使用ConfigurableApplicationContext接口的close()方法关闭ApplicationContext容器时触发该事件。
4、 ContextStopedEvent: 当使用ConfigurableApplicationContext接口的stop()方法停止ApplicationContext容器时触发该事件。


作者:献给记性不好的自己
链接:http://www.jianshu.com/p/01f7a971a4b9
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

spring boot容器加载完后执行特定操作的更多相关文章

  1. spring启动容器加载成功后执行调用方法

    需求: 由于在微服务架构中各服务之间都是通过接口调用来进行交互的,像很多的基础服务,类似字典信息其实并不需每次需要的时候再去请求接口.所以我的想法是每次启动项目的时候,容器初始化完成,就去调用一下基础 ...

  2. js页面加载完后执行(document.onreadystatechange 和 document.readyState)

    js页面加载完后执行javascript(document.onreadystatechange 和 document.readyState) document.onreadystatechange ...

  3. spring 容器加载完成后执行某个方法

    理论 刚好再开发过程中遇到了要在项目启动后自动开启某个服务,由于使用了spring,我在使用了spring的listener,它有onApplicationEvent()方法,在Spring容器将所有 ...

  4. 图片加载完后执行js

    <script>            window.onload=function(){                          var liwidth = $('.imgul ...

  5. ExtJS 等待两个/多个store加载完再执行操作的方法

    ExtJS是一种主要用于创建前端用户界面,是一个基本与后台技术无关的前端ajax框架. Extjs加载Store是异步加载的,这有很多好处.但是当我们要在两个或多个不同的store加载完再执行一些操作 ...

  6. 在Spring Boot中加载初始化数据

    文章目录 依赖条件 data.sql文件 schema.sql 文件 @sql注解 @SqlConfig 注解 在Spring Boot中加载初始化数据 在Spring Boot中,Spring Bo ...

  7. js中页面加载完成后执行的几种方式及执行顺序

    1:使用jQuery的$(function){}; 2:使用jquery的$(document).ready(function(){});前两者本质上没有区别,第1种是第2种的简写方式.两个是docu ...

  8. spring boot启动加载项CommandLineRunner

    spring boot启动加载项CommandLineRunner 在使用SpringBoot构建项目时,我们通常有一些预先数据的加载.那么SpringBoot提供了一个简单的方式来实现–Comman ...

  9. window.onload在文档加载完成后执行

    验证a .b两点疑惑: a.<script src="./main.js"></script>中的window.onload是在html全部加载完了才执行, ...

随机推荐

  1. jquery appendTo用法

    $("#top_cartWarp").appendTo($("#top_main_right")).css('position','relative').css ...

  2. 微信小程序button选中改样式-实现单选/多选

    小程序实现多button单选/多选 红色为选中状态 单选 多选 ①wxss /* pages/button-select/button-select.wxss */ .button_container ...

  3. golang label breaks

    我们在for多层嵌套时,有时候需要直接跳出所有嵌套循环, 这时候就可以用到go的label breaks特征了. 先看一个范例代码: package main import (     "f ...

  4. RequireJS 是一个JavaScript模块加载器

    RequireJS 是一个JavaScript模块加载器.它非常适合在浏览器中使用, 它非常适合在浏览器中使用,但它也可以用在其他脚本环境, 就像 Rhino and Node. 使用RequireJ ...

  5. drupal7 获取当前使用的主题的名称

    直接引用全局变量就行: 参考: 代码测试: global $theme, $theme_key; echo $theme; echo '<br>'; echo $theme_key; 结果 ...

  6. 关于 webpack 跨域

    一.使用  http-proxy-middleware  代理 安装 http-proxy-middleware 依赖 在src 目录下 新建一个 setupProxy.js文件 // 引用依赖 va ...

  7. <Android 基础(二十四)> EditText

    介绍 A text field allows the user to type text into your app. It can be either single line or multi-li ...

  8. .NET AOP微型框架发布 --CleanAOP

    CleanAOP--简介 作者:立地(欧文) 邮箱:jarvin_g@126.com 导语: AOP为Aspect Oriented Programming的缩写. 意为:面向切面编程.将日志记录,性 ...

  9. gulp 在 angular 项目中的使用

    gulp 在 angular 项目中的使用 keyword:gulp,angularjs,ng,ngAnnotate,jshint,gulpfile 最后附完整简洁的ng项目gulpfile.js 准 ...

  10. 【转】OmniGraffle (一)从工具栏开始

    原文链接:http://www.jianshu.com/p/52f3ecbe8f2d OmniGraffle的软件布局和大多数图形类软件类似,中间是编辑区,左边是页面和对象组织的管理,右边是参数设置和 ...