Spring Boot学习--项目启动时执行指定service的指定方法
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; @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; /**
* 这里通过设定value的值来指定执行顺序
*/
@Component
@Order(value = 1)
public class MyApplicationRunner implements ApplicationRunner{ @Override
public void run(ApplicationArguments var1) throws Exception{
System.out.println("MyApplicationRunner1!");
}
}
Spring Boot学习--项目启动时执行指定service的指定方法的更多相关文章
- Spring Boot学习--项目启动时执行特定方法
Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动 ...
- 在web项目启动时执行某个方法
在web项目中有很多时候需要在项目启动时就执行一些方法,而且只需要执行一次,比如:加载解析自定义的配置文件.初始化数据库信息等等,在项目启动时就直接执行一些方法,可以减少很多繁琐的操作. 在工作中遇到 ...
- SpringBoot 源码解析 (三)----- Spring Boot 精髓:启动时初始化数据
在我们用 springboot 搭建项目的时候,有时候会碰到在项目启动时初始化一些操作的需求 ,针对这种需求 spring boot为我们提供了以下几种方案供我们选择: ApplicationRunn ...
- 一张图帮你记忆,Spring Boot 应用在启动阶段执行代码的几种方式
前言 有时候我们需要在应用启动时执行一些代码片段,这些片段可能是仅仅是为了记录 log,也可能是在启动时检查与安装证书 ,诸如上述业务要求我们可能会经常碰到 Spring Boot 提供了至少 5 种 ...
- Spring在Web容器启动时执行初始化方法
需求:在tomcat启动时开启一个定时任务. 想法:容器启动时执行方法,最容易想到的就是servlet中可以配置load-on-startup,设置一个正整数也就可以随容器一起启动. 问题:上面的方法 ...
- 15 个优秀开源的 Spring Boot 学习项目,一网打尽!
Spring Boot 算是目前 Java 领域最火的技术栈了,松哥年初出版的 <Spring Boot + Vue 全栈开发实战>迄今为止已经加印了 8 次,Spring Boot 的受 ...
- 15 个优秀开源的 Spring Boot 学习项目
Spring Boot 算是目前 Java 领域最火的技术栈了,松哥年初出版的 <Spring Boot + Vue 全栈开发实战>迄今为止已经加印了 8 次,Spring Boot 的受 ...
- spring注解之@PostConstruct在项目启动时执行指定方法
一.注解解释 Spring的@PostConstruct注解在方法上,表示此方法是在Spring实例化该Bean之后马上执行此方法,之后才会去实例化其他Bean,并且一个Bean中@PostConst ...
- Java项目启动时执行指定方法的几种方式
很多时候我们都会碰到需要在程序启动时去执行的方法,比如说去读取某个配置,预加载缓存,定时任务的初始化等.这里给出几种解决方案供大家参考. 1. 使用@PostConstruct注解 这个注解呢,可以在 ...
随机推荐
- P5270 无论怎样神树大人都会删库跑路
题目地址:P5270 无论怎样神树大人都会删库跑路 第一眼看上去是模拟,似乎是 \(O(n)\) 的 水题 信心满满的写完: #include <bits/stdc++.h> using ...
- 手写代码 - java.lang.String/StringBuilder 相关
语言:Java 9-截取某个区间的string /** * Returns a string that is a substring of this string. The * substring b ...
- LwIP Application Developers Manual12---Configuring lwIP
1.前言 2.LwIP makefiles With minimal features C_SOURCES = \ src/api/err.c \ src/core/init.c \ src/core ...
- TreeGrid 控件集 :delphi 学习群 ---- 166637277 (Delphi学习交流与分享)
delphi 学习群: 166637277 (Delphi学习交流与分享). 群主QQ: 1936431438 TreeGrid 控件集 收集: 1.https://www.lmd.de/produ ...
- shell 在手分析服务器日志【转】
自己的小网站跑在阿里云的 ECS 上面, 偶尔也去分析分析自己网站服务器日志,看看网站的访问量.看看有没有黑阔搞破坏!于是收集,整理一些服务器日志分析命令,大家可以试试! awk '{print $1 ...
- 题解-ZJOI2015地震后的幻想乡
Problem bzoj & 洛谷 题意简述:给定一个\(n\)(\(n\leq 10\))个点\(m\)条边的无向图,每条边的权值为一个\(0\)到\(1\)之间的连续随机变量,求图的最小生 ...
- 题解-CodeForces700E Cool Slogans
Problem 题目链接 题目大意:给定一个字符串,每次取出出现至少两次的子串替换原串,问最多能替换多少次,输出答案加一(字符串长为\(2×10^5\)) Solution 前置技能:SAM.线段树合 ...
- 第八次作业(课堂实战)- 项目UML设计(团队)
1. 团队信息 队名:小白吃队 成员: 后敬甲 031602409 卢泽明 031602328 蔡文斌 031602301 葛亮 031602617 刘浩 031602423 黄泽 031602317 ...
- $Django 表设计,登陆图片验证
pip3 install pillow #PIL登陆图片验证(未实现局部刷新)详细:https://www.cnblogs.com/qiangyuge/p/8025168.htmldef get_co ...
- C/C++中如何在main()函数之前执行一条语句?
在C语言中,如果使用GCC的话,可以通过attribute关键字声明constructor和destructor(C语言中如何在main函数开始前执行函数) #include <stdio.h& ...