Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner。

这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方法。我们可以通过实现ApplicationRunner和CommandLineRunner,来实现,他们都是在SpringApplication 执行之后开始执行的。

CommandLineRunner接口可以用来接收字符串数组的命令行参数,ApplicationRunner 是使用ApplicationArguments 用来接收参数的,貌似后者更牛逼一些。

先看看CommandLineRunner :

package com.springboot.study;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component; /**
* Created by pangkunkun on 2017/9/3.
*/
@Component
public class MyCommandLineRunner implements CommandLineRunner{ @Override
public void run(String... var1) throws Exception{
System.out.println("This will be execute when the project was started!");
}
}
  • ApplicationRunner :
package com.springboot.study;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component; /**
* Created by pangkunkun on 2017/9/3.
*/
@Component
public class MyApplicationRunner implements ApplicationRunner { @Override
public void run(ApplicationArguments var1) throws Exception{
System.out.println("MyApplicationRunner class will be execute when the project was started!");
} }

这两种方式的实现都很简单,直接实现了相应的接口就可以了。记得在类上加@Component注解。

如果想要指定启动方法执行的顺序,可以通过实现org.springframework.core.Ordered接口或者使用org.springframework.core.annotation.Order注解来实现。

这里我们以ApplicationRunner 为例来分别实现。

Ordered接口:

package com.springboot.study;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component; /**
* Created by pangkunkun on 2017/9/3.
*/
@Component
public class MyApplicationRunner implements ApplicationRunner,Ordered{ @Override
public int getOrder(){
return 1;//通过设置这里的数字来知道指定顺序
} @Override
public void run(ApplicationArguments var1) throws Exception{
System.out.println("MyApplicationRunner1!");
} }

Order注解实现方式:

package com.springboot.study;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* Created by pangkunkun on 2017/9/3.
* 这里通过设定value的值来指定执行顺序
*/
@Component
@Order(value = 1)
public class MyApplicationRunner implements ApplicationRunner{ @Override
public void run(ApplicationArguments var1) throws Exception{
System.out.println("MyApplicationRunner1!");
} }

这里不列出其他对比方法了,自己执行下就好。

Spring Boot学习--项目启动时执行特定方法的更多相关文章

  1. Spring Boot学习--项目启动时执行指定service的指定方法

    Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方 ...

  2. 在web项目启动时执行某个方法

    在web项目中有很多时候需要在项目启动时就执行一些方法,而且只需要执行一次,比如:加载解析自定义的配置文件.初始化数据库信息等等,在项目启动时就直接执行一些方法,可以减少很多繁琐的操作. 在工作中遇到 ...

  3. spring注解之@PostConstruct在项目启动时执行指定方法

    一.注解解释 Spring的@PostConstruct注解在方法上,表示此方法是在Spring实例化该Bean之后马上执行此方法,之后才会去实例化其他Bean,并且一个Bean中@PostConst ...

  4. Spring在Web容器启动时执行初始化方法

    需求:在tomcat启动时开启一个定时任务. 想法:容器启动时执行方法,最容易想到的就是servlet中可以配置load-on-startup,设置一个正整数也就可以随容器一起启动. 问题:上面的方法 ...

  5. Spring项目启动时执行初始化方法

    一.applicationContext.xml配置bean <bean id="sensitiveWordInitUtil" class ="com.hx.daz ...

  6. Java项目启动时执行指定方法的几种方式

    很多时候我们都会碰到需要在程序启动时去执行的方法,比如说去读取某个配置,预加载缓存,定时任务的初始化等.这里给出几种解决方案供大家参考. 1. 使用@PostConstruct注解 这个注解呢,可以在 ...

  7. spring 在web容器启动时执行初始化方法

    开发框架:spingMVC+myBatis 解决方案:给web容器添加一个Listener类,在容器启动的时候执行Listener的“初始化”方法,在这个初始化方法中执行查询数据库的所有操作,然后将数 ...

  8. java项目启动时执行指定方法

    想到的就是监听步骤如下: 1.配置web.xml <listener> <listener-class>com.listener.InitListener</listen ...

  9. SpringBoot 源码解析 (三)----- Spring Boot 精髓:启动时初始化数据

    在我们用 springboot 搭建项目的时候,有时候会碰到在项目启动时初始化一些操作的需求 ,针对这种需求 spring boot为我们提供了以下几种方案供我们选择: ApplicationRunn ...

随机推荐

  1. Dataframe 新增一列, apply 通用方法

    df['c'] = df.apply(lambda row: 1 if row['a'] < 0 and row['b'] > 0 else 0, axis=1) apply 是一个好方法 ...

  2. shell学习笔记汇总

    1.shell脚本中函数使用 函数定义在前,调用在后,顺序反了就没有效果了.函数调用为:函数名 参数列表 函数内部通过以下变量访问函数的参数:shell脚本函数中: $0: 这个脚本的名字 $n: 这 ...

  3. Entities、pads、links 实体、垫、链接

    Entities.pads.links 实体.垫.链接 Entities:1.实体由一个struct media_entity实例表示.结构通常嵌入到一个较高级别的结构,例如v4l2_subdev或v ...

  4. centos7.x网卡bond配置

    本文摘抄自 https://www.cnblogs.com/liwanggui/p/6807212.html centos7网卡bond配置 centos7网卡bond配置 1 备份网卡配置文件2 使 ...

  5. vuex 知识点

    Action 类似于 mutation,不同在于: 1.Action 提交的是 mutation,而不是直接变更状态. 2.Action 可以包含任意异步操作. mutation是同步的,当需要异步操 ...

  6. codevs2189数字三角形(%100)

    题目:http://codevs.cn/problem/2189/ %100的话就加一维状态.把最优性改为可行性(存在性). #include<iostream> #include< ...

  7. BeanUtils Object 取值赋值

    /** * 将结果集导出为Excel * * @param response * @param fsc * @param columns * @param bizType * @throws Exce ...

  8. C语言扩展动态内存报错:realloc(): invalid next size: 0x0000000002365010 ***

    晚上被这个内存扩展崩溃的问题折腾的有点崩溃,当答案揭晓的那一刻,恍然大悟,原来如此简单. 练习题目:输入一个字符串,根据字母进行排序,说白了就是一个简单的冒泡 #include <stdio.h ...

  9. mysql视图 新手的问答

    ☺ζั͡ޓއއއ๓º♥双٩(•(365335093) 16:03:02 创建的视图能保存多长时间,保存在哪啊 上天&宠儿(961431958) 16:08:59 数据库 上天&宠儿(9 ...

  10. 跨域获取json数据

    原文地址:http://my.oschina.net/LinBandit/blog/34570   前阵子做了一个前端动态加载json数据的应用,其中使用xmlhttprequest动态加载js,但是 ...