Spring Boot学习--项目启动时执行特定方法
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学习--项目启动时执行特定方法的更多相关文章
- Spring Boot学习--项目启动时执行指定service的指定方法
Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方 ...
- 在web项目启动时执行某个方法
在web项目中有很多时候需要在项目启动时就执行一些方法,而且只需要执行一次,比如:加载解析自定义的配置文件.初始化数据库信息等等,在项目启动时就直接执行一些方法,可以减少很多繁琐的操作. 在工作中遇到 ...
- spring注解之@PostConstruct在项目启动时执行指定方法
一.注解解释 Spring的@PostConstruct注解在方法上,表示此方法是在Spring实例化该Bean之后马上执行此方法,之后才会去实例化其他Bean,并且一个Bean中@PostConst ...
- Spring在Web容器启动时执行初始化方法
需求:在tomcat启动时开启一个定时任务. 想法:容器启动时执行方法,最容易想到的就是servlet中可以配置load-on-startup,设置一个正整数也就可以随容器一起启动. 问题:上面的方法 ...
- Spring项目启动时执行初始化方法
一.applicationContext.xml配置bean <bean id="sensitiveWordInitUtil" class ="com.hx.daz ...
- Java项目启动时执行指定方法的几种方式
很多时候我们都会碰到需要在程序启动时去执行的方法,比如说去读取某个配置,预加载缓存,定时任务的初始化等.这里给出几种解决方案供大家参考. 1. 使用@PostConstruct注解 这个注解呢,可以在 ...
- spring 在web容器启动时执行初始化方法
开发框架:spingMVC+myBatis 解决方案:给web容器添加一个Listener类,在容器启动的时候执行Listener的“初始化”方法,在这个初始化方法中执行查询数据库的所有操作,然后将数 ...
- java项目启动时执行指定方法
想到的就是监听步骤如下: 1.配置web.xml <listener> <listener-class>com.listener.InitListener</listen ...
- SpringBoot 源码解析 (三)----- Spring Boot 精髓:启动时初始化数据
在我们用 springboot 搭建项目的时候,有时候会碰到在项目启动时初始化一些操作的需求 ,针对这种需求 spring boot为我们提供了以下几种方案供我们选择: ApplicationRunn ...
随机推荐
- linux系统的启动过程及系统初始化
(其中/etc/inittab是一个很重要的文件,值得细究http://www.2cto.com/os/201108/98426.html) 其开头的446字节内容特指为"主引导记录&quo ...
- TensorFlow笔记-02-Windows下搭建TensorFlow环境(win版非虚拟机)
TensorFlow笔记-02-Windows下搭建TensorFlow环境(win版非虚拟机) 本篇介绍的是在windows系统下,使用 Anaconda+PyCharm,不使用虚拟机,也不使用 L ...
- 和为 s 的两个数字(和为 s 的连续正数序列)
题目 输入一个递增排序的数组和一个数字 s,在数组中查找两个数,得它们的和正好是 s.如果有多对数字的和等于 s,输出任意一对即可 思路 我们先在数组中选择两个数字,如果它们的和等于输入的 s,我们就 ...
- spring 核心思想:AOP 理解
什么是AOP? AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 面向切面编程Aspect-Orlented-Programming,即AO ...
- scala的hello world出现的问题
build出现: Error:scalac: Error: org.jetbrains.jps.incremental.scala.remote.ServerExceptionError compil ...
- oracle锁表查询
ORACLE EBS操作某一个FORM界面,或者后台数据库操作某一个表时发现一直出于"假死"状态,可能是该表被某一用户锁定,导致其他用户无法继续操作 复制代码代码如下: --锁表查 ...
- django orm 常用查询筛选
大于.大于等于 __gt 大于 __gte 大于等于 User.objects.filter(age__gt=10) // 查询年龄大于10岁的用户 User.objects.filter(age__ ...
- bzoj1047 理想的正方形
Description 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. Input 第一行为3个整数,分别表示a,b,n的值第二行至第 ...
- sikuli 安装
1 简介 官方介绍: Sikuli脚本自动化,你在屏幕上看到的任何东西.它使用图像识别,识别和控制GUI组件.这是有用的,当有一个GUI的内部或源代码的访问是不容易的. Sikuli(在墨西哥 ...
- 汽车收费 C++ PTA
7-1 汽车收费(10 分) 现在要开发一个系统,管理对多种汽车的收费工作. 给出下面的一个基类框架 class Vehicle { protected: string NO;//编号 public: ...