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 ...
随机推荐
- eos开发(二)使用cleos命令行客户端操作EOS(钱包wallet基础操作)
不知道下边这一段英文你们是不是能看懂,如果看不懂那就算了,我就是转过来随便看看的. 总之你记住nodeos.cleos和keosd这三个工程十分重要就行了,回头咱们的研究都从这三个工程杀进去. EOS ...
- C++版 - 剑指offer 面试题5:从尾到头打印链表 题解
面试题5:从尾到头打印链表 提交网址: http://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035?tpId=13&tq ...
- 征服诱人的Vagrant!
一.背景 最近要开始深入学习分布式相关的东西了,那第一步就是在自己的电脑上安装虚拟机,以前在Windows平台,我选择用VMware Workstation作为虚拟机软件,现在在Mac系统下,感觉 ...
- DateTimeHelper
本文参考文章: http://geekswithblogs.net/mnf/articles/my-datetimehelper-class.aspx https://referencesource. ...
- win8 notepad++ 设置无法保存
前些天买了新笔记本.装的是win8 ,后来装了notepad++ ,最新版的,改了字体,下次从新打开之后,发现字体改动无效.后来试了一下“管理员身份运行”,再次打开,保存就有效.但总不能每次都那样去右 ...
- zookeeper实现动态感知服务器上下线
在实际的生产环境中我们一般都是集群环境部署的,同一个程序我们会部署在相同的几台服务器中,这时我们可以通过负载均衡服务器去调度,但是我们并不能很快速的获知哪台服务器挂掉了,这时我们就可以使用zook ...
- MySQL中间件之ProxySQL(12):禁止多路路由
返回ProxySQL系列文章:http://www.cnblogs.com/f-ck-need-u/p/7586194.html 1.multiplexing multiplexing,作用是将语句分 ...
- C#字符串。
string类型不能被继承,它是密封类,sealed. 一.字符串的特性. 1.不可变性. class Program { static void Main(string[] args) { stri ...
- 35.QT-多线程
程序和进程的区别 进程是动态的,程序是静态的,进程是程序运行时的实例,是占用系统运行资源的程序 进程是暂时的,程序是永久的, 进程是通过程序运行时得到的 程序是一个数据文件,进程是内存中动态的运行实体 ...
- spring2.0:The server time zone value 'Ãùú±êüñ¼ä' is unrecognized or represents more than one time zone. You must configure either th
提示系统时区出现错误,可以在mysql中执行命令: set global time_zone='+8:00' 或者在数据库驱动的url后加上serverTimezone=UTC参数 jdbc:mysq ...