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 ...
随机推荐
- Java使用OkHttps工具类调用外部接口
前言 现在公司业务已止不是传统的增删改查等简单的业务处理操作,而是对数据各种联调三方接口与其他系统进行交互等,那么就需要我们在后台java中进行外部接口的调用,本文采用OkHttps工具类对接微信接口 ...
- strace命令用法
-tt 在每行输出的前面,显示毫秒级别的时间 -T 显示每次系统调用所花费的时间 -v 对于某些相关调用,把完整的环境变量,文件stat结构等打出来. -f 跟踪目标进程,以及目标进程创建的所有子进程 ...
- java.util.regex包下的Pattern类和Matcher类的使用总结
一.介绍 Java正则表达式通过java.util.regex包下的Pattern类与Matcher类实现1.Pattern类用于创建一个正则表达式,也可以说创建一个匹配模式,它的构造方法是私有的,不 ...
- vue android低版本 白屏问题 你是不是用了Object.assign ??
问题描述 在部分比较低版本的手机中,发现apk安装后白屏,但是大部分手机都能安装. 本人在使用android4.4时候,也是安装后打开白屏. 原因: 代码写法不兼容 this.user = Objec ...
- 【jQuery】(3)---Jquery操作Dom
1 内部插入节点 <body> <ul id="city"> <li id="bj" name=&qu ...
- 【云服务器部署】---Linux下安装nginx
[云服务器部署]---Linux下安装nginx 之前两篇,分别讲了:Linux下安装MySQL 和 springboot项目部署云服务器 nginx安装也是挺简单的.具体步骤如下: 第一步,下载 ...
- CentOS7 Hadoop 3.1.0 编译安装
1.配置环境变量 JAVA_HOME=/jdk1..0_131 ANT_HOME=/apache-ant- MAVEN_HOME=/apache-maven- FINDBUGS_HOME=/findb ...
- python 时间模块time,datetime
模块(module)是 Python 中非常重要的东西,你可以把它理解为 Python 的扩展工具.换言之,Python 默认情况下提供了一些可用的东西,但是这些默认情况下提供的还远远不能满足编程实践 ...
- MySQL中间件之ProxySQL(13):ProxySQL集群
返回ProxySQL系列文章:http://www.cnblogs.com/f-ck-need-u/p/7586194.html ProxySQL有原生的集群功能,但是这个原生的集群功能还正在试验阶段 ...
- .NET CORE 实践(3)--Visual Studio 2015 Update 3更新之后DotNetCore.1.0.1-VS2015Tools.Preview2.0.2.exe无法正确安装
打开 https://www.microsoft.com/net/core#windows,点击 https://go.microsoft.com/fwlink/?LinkId=691129下载vs2 ...