springboot 学习之路 9 (项目启动后就执行特定方法)
目录:【持续更新。。。。。】
spring 部分常用注解
spring boot 学习之路1(简单入门)
spring boot 学习之路2(注解介绍)
spring boot 学习之路3( 集成mybatis )
spring boot 学习之路4(日志输出)
spring boot 学习之路5(打成war包部署tomcat)
spring boot 学习之路6(定时任务)
spring boot 学习之路6(集成durid连接池)
spring boot 学习之路7(静态页面自动生效问题)
spring boot 学习之路8 (整合websocket(1))
spring boot 学习之路9 (项目启动后就执行特定方法)
概述:
spring boot 整理之路继续前行,这次我说一下怎么在spring boot项目启动后立即执行某一个特定的方法,已经要执行特定方法不止一个的时候的顺序问题【顺序问题我只说注解咋实现@Order】
分析:
这次我拿项目中某一个功能为案例,进行演示:
如何在项目启动时启动ftp服务器(java内嵌ftp服务器):如何在spring boot项目启动时让ftp服务器也随之启动呢?
方案:
针对上面的问题,我在spring boot中找到了解决方案。那就是spring boot给我们提供了两个接口 ApplicationRunner和CommandLineRunner。这两个接口是Springboot给我们提供了两种“开机启动”的方式
ApplicationRunner :
源码如下:
public interface ApplicationRunner {
void run(ApplicationArguments var1) throws Exception;
}
CommandLineRunner :
源码如下:
public interface CommandLineRunner {
void run(String... var1) throws Exception;
} 对比:
相同点:这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方法。我们可以通过实现ApplicationRunner和CommandLineRunner,来实现,他们都是在SpringApplication 执行之后开始执行的。
不同点:CommandLineRunner接口可以用来接收字符串数组的命令行参数,ApplicationRunner 是使用ApplicationArguments 用来接收参数的 【根据业务场景灵活运用】
代码:
注意:在这我用了注解,如果选择接口实现方式只需要实现Order接口即可,里面有个getOrder方法 返回个int值,值越小,越优先执行
/**
* @Author:huhy
* @DATE:Created on 2018/1/3 12:47
* @Modified By:
* @Class 项目启动后运行此方法:CommandLineRunner实现
*/
@Component
@Order(value=2)
public class FtpInitRunner implements CommandLineRunner {
@Override
/**
*@Author: huhy
*@Package_name:com.huhy.web.common.runner
*@Date:13:20 2018/1/3
*@Description:项目启动后运行此方法 CommandLineRunner
* //项目路径 path = E:\IDE\workspace\ideaWorkspace\spring boot\spring-boot
String path = System.getProperty("user.dir");
CommandLine.main(new String[]{path+"\\src\\main\\resources\\ftpserver\\ftpd-typical.xml"});
System.out.println("----------------------"+path);
*/
public void run(String... var1) throws Exception{
System.out.println("2222222222222222222222");
}
/**
* @Author:huhy
* @DATE:Created on 2018/1/3 13:20
* @Modified By:
* @Class Description:ApplicationRunner 实现
*/
@Component
@Order(value = 1) //执行顺序控制
public class FtpInitRunner2 implements ApplicationRunner{
@Override
/**
*@Author: huhy
*@Package_name:com.huhy.web.common.runner
*@Date:13:29 2018/1/3
*@Description: ApplicationRunner方式实现
*/
public void run(ApplicationArguments applicationArguments) throws Exception {
//项目路径 path = E:\IDE\workspace\ideaWorkspace\spring boot\spring-boot
String path = System.getProperty("user.dir");
CommandLine.main(new String[]{path+"\\src\\main\\resources\\ftpserver\\ftpd-typical.xml"});
System.out.println("----------------------"+path);
System.out.println("111111111111111111111111"); }
启动图如下:

总结:
spring boot的项目启动注意分为两步:
实现相应接口(ApplicationRunner和CommandLineRunner) -----------》 加入注解 (@Component | @Order )
springboot 学习之路 9 (项目启动后就执行特定方法)的更多相关文章
- 在web项目启动时,执行某个方法
在web项目中有很多时候需要在项目启动时就执行一些方法,而且只需要执行一次,比如:加载解析自定义的配置文件.初始化数据库信息等等,在项目启动时就直接执行一些方法,可以减少很多繁琐的操作. 在工作中遇到 ...
- springboot 学习之路 8 (整合websocket(1))
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 1(简单入门)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 3( 集成mybatis )
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 6(定时任务)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 2(注解介绍)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 4(日志输出)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 5(打成war包部署tomcat)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 6(集成durid连接池)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
随机推荐
- Saiku多用户使用时数据同步刷新(十七)
Saiku多用户使用时数据同步刷新 这里我们需要了解一下关于saiku的刷新主要有两种数据需要刷新: >1 刷新数据库的表中的数据,得到最新的表数据进行展示. >2 刷新cube信息,得到 ...
- Word文档使用密码加密
Word文档使用密码加密 方法如下: 文件-->信息-->保护文档-->用密码进行加密-->设置密码
- MapReduce中的Join
一. MR中的join的两种方式: 1.reduce side join(面试题) reduce side join是一种最简单的join方式,其主要思想如下: 在map阶段,map函数同时读取两个文 ...
- 一条sql解决.一张表的数据复制到另外一张表
如何把一个表的数组复制到一张表?也许很多人会把这个表查出来的数据再插入到另外一张表里面,这样很麻烦又要写代码逻辑去处理,其实一条sql语句就可以把一张表的数据复制到另外一张表,或者一张表的某一条数据复 ...
- PHP源码阅读(一):str_split函数
注:源码版本:php5.6.33. 函数简介 str_split 原型: array str_split ( string $string [, int $split_length = 1 ] ) 说 ...
- LAMP环境快速搭建
1.安装httpd [root@localhost httpd]# yum -y install httpd #安装httpd [root@localhost httpd]# chkconfig ht ...
- python字符串(string)方法整理
python中字符串对象提供了很多方法来操作字符串,功能相当丰富. print(dir(str)) [..........'capitalize', 'casefold', 'center', 'co ...
- 阿里云IoT
阿里云IoT: https://iot.aliyun.com/
- 【转载】Window服务器开机后一直处于蓝色屏幕(非蓝屏 crash)状态
阿里云Windows系统服务器运维的过程中,有时候会遇到实例开机后一直处于蓝色背景屏幕(非蓝屏 crash )状态.此时你发现鼠标可以任意正常移动,但是屏幕上却没有任何的图标可以供操作,这种情况可能是 ...
- SET NOCOUNT { ON | OFF }
当 SET NOCOUNT 为 ON 时,不返回计数(表示受 Transact-SQL 语句影响的行数) SET NOCOUNT 为 ON 时,也更新 @@ROWCOUNT 函数. 当 SET NOC ...