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 ...
随机推荐
- Windows下安装及使用NVM
Windows下安装及使用NVM 所谓nvm就是一个可以让你在同一台机器上安装和切换不同版本node的工具.这里是一篇安装及使用教程. 第一步:下载nvm 可以到github上下载最新版本https: ...
- 【翻译】JavaScript中5个值得被广泛使用的数组方法
原文地址:http://colintoh.com/blog/5-array-methods-that-you-should-use-today?utm_source=javascriptweekly& ...
- sql server 性能调优之 当前用户请求分析 (1)
一. 概述 在生产数据库运行期间,有时我们需要查看当前用户会话状态或者是说数据库当前是否运行良好, 应用的场景比如:当运行的应用系统响应突然变慢时需要分析数据库的.或想分析当前的数据库是否繁忙,是否有 ...
- HTTP的基本原理
用户访问万维网文档,万维网文档之间的链接以及万维网文档中数据传送到用户计算机,这些功能的实现都是由超文本传输协议 HTTP(HyperTextTransfer Protocol) 负责完成的. HTT ...
- How Tomcat works — 三、tomcat启动(2)
在了解了tomcat 的一些基本组件之后,学习启动过程就更容易理解了,因为启动过程就是启动各个组件. 目录 启动顺序 Bootstrap类 Catalina类 StandardServer类和Stan ...
- kubernetes之收集集群的events,监控集群行为
一.概述 线上部署的k8s已经扛过了双11的洗礼,期间先是通过对网络和监控的优化顺利度过了双11并且表现良好.先简单介绍一下我们kubernetes的使用方式: 物理机系统:Ubuntu-16.04( ...
- Mybatis源码解析-MapperRegistry代理注册mapper接口
知识储备 SqlsessionFactory-mybatis持久层操作数据的前提,具体的解析是通过SqlSessionFactoryBean生成的,可见>>>Spring mybat ...
- 痞子衡嵌入式:备受开源社区推崇的分布式版本控制工具(Git)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是分布式版本控制工具Git. 1.为什么需要版本控制系统? 单人软件项目开发过程,往往很多功能都是逐步增加的,在代码开发过程中,有的时候功 ...
- Spring Boot中如何扩展XML请求和响应的支持
在之前的所有Spring Boot教程中,我们都只提到和用到了针对HTML和JSON格式的请求与响应处理.那么对于XML格式的请求要如何快速的在Controller中包装成对象,以及如何以XML的格式 ...
- 响应者链UIResponder-扩大UIButton的点击范围
在开发中,我们经常看到有按钮等的点击,会出现响应事件.按钮->view->ViewController->UIWindow->UIApplication,这就形成了一个响应链. ...