SpringBoot项目启动时执行初始化操作
SpringBooot中的CommandLineRunner接口会在所有Spring Beans初始化之后,SpringApplication.run()之前执行。
1.添加pom引用
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.itstudy</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version> <name>demo</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.添加两个CommandLineRunner
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Component
@Order(2) //指定顺序
public class Runner1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Runner1 ===========");
}
}
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Component
@Order(1) //指定顺序
public class Runner2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Runner2 ===========");
}
}
3.启动项目
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class App { public static void main(String[] args) { System.out.println("启动中..."); SpringApplication.run(App.class, args); System.out.println("启动成功");
}
}
4.运行结果
启动中... . ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.9.RELEASE) 2019-06-11 09:49:12.152 INFO 4536 --- [ main] com.itstudy.App : Starting App on liuwma with PID 4536 (D:\javawork\ideawork\runnerdemo\target\classes started by Administrator in D:\javawork\ideawork)
2019-06-11 09:49:12.156 INFO 4536 --- [ main] com.itstudy.App : No active profile set, falling back to default profiles: default
2019-06-11 09:49:12.247 INFO 4536 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5123a213: startup date [Tue Jun 11 09:49:12 CST 2019]; root of context hierarchy
2019-06-11 09:49:13.277 INFO 4536 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
Runner2 ===========
Runner1 ===========
2019-06-11 09:49:13.300 INFO 4536 --- [ main] com.itstudy.App : Started App in 1.604 seconds (JVM running for 2.483)
启动成功
SpringBoot项目启动时执行初始化操作的更多相关文章
- SpringBoot程序启动时执行初始化代码
因项目集成了Redis缓存部分数据,需要在程序启动时将数据加载到Redis中,即初始化数据到Redis. 在SpringBoot项目下,即在容器初始化完毕后执行我们自己的初始化代码. 第一步:创建实现 ...
- Spring项目启动时执行初始化方法
一.applicationContext.xml配置bean <bean id="sensitiveWordInitUtil" class ="com.hx.daz ...
- 在web项目启动时执行某个方法
在web项目中有很多时候需要在项目启动时就执行一些方法,而且只需要执行一次,比如:加载解析自定义的配置文件.初始化数据库信息等等,在项目启动时就直接执行一些方法,可以减少很多繁琐的操作. 在工作中遇到 ...
- Springboot 项目启动后执行某些自定义代码
Springboot 项目启动后执行某些自定义代码 Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRun ...
- SpringBoot项目启动时链接数据库很慢
SpringBoot项目启动时链接数据库很慢 springboot项目在启动时候,如下图所示,链接数据库很慢 解决方法:在mysql 的配置文件中 配置 skip-name-resolve
- Spring在Web容器启动时执行初始化方法
需求:在tomcat启动时开启一个定时任务. 想法:容器启动时执行方法,最容易想到的就是servlet中可以配置load-on-startup,设置一个正整数也就可以随容器一起启动. 问题:上面的方法 ...
- Java项目启动时执行指定方法的几种方式
很多时候我们都会碰到需要在程序启动时去执行的方法,比如说去读取某个配置,预加载缓存,定时任务的初始化等.这里给出几种解决方案供大家参考. 1. 使用@PostConstruct注解 这个注解呢,可以在 ...
- Spring Boot学习--项目启动时执行指定service的指定方法
Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方 ...
- spring注解之@PostConstruct在项目启动时执行指定方法
一.注解解释 Spring的@PostConstruct注解在方法上,表示此方法是在Spring实例化该Bean之后马上执行此方法,之后才会去实例化其他Bean,并且一个Bean中@PostConst ...
随机推荐
- safari兼容时间格式 NAN
问题: safari中不支持2018-08-01这种格式转为时间戳会显示NaN 方案: new Date('2018/08/01').getTime() 将-转化为/ // 正则替换 new Date ...
- Python开发WebService:REST,web.py,eurasia,Django
Python开发WebService:REST,web.py,eurasia,Django 博客分类: Python PythonRESTWebWebServiceDjango 对于今天的WebSe ...
- SpringBoot + kaptcha 生成、校对 验证码
1.引入 kaptcha 的 Maven 依赖 <dependency> <groupId>com.github.penggle</groupId> <art ...
- 用class语法派生Enum并增加描述值的类属性来定义一个新枚举
import enum class BugStatus(enum.Enum): new = 7 incomplete = 6 invalid = 5 wont_fix ...
- vue 路由拦截器和请求拦截器
路由拦截器 已路由为导向 router.beforeEach((to,from,next)=>{ if(to.path=='/login' || localStorage.getItem('to ...
- android出现backtrace 定位方法
- LSTM细节
为什么使用tanh? 为了克服梯度消失问题,我们需要一个二阶导数在趋近零点之前能维持很长距离的函数.tanh是具有这种属性的合适的函数. 为什么要使用Sigmoid? 由于Sigmoid函数可以输出0 ...
- pd.read_csv参数解析
对pd.read_csv参数做如下解释: pandas.read_csv(filepath_or_buffer, sep=', ', delimiter=None, header='infer', n ...
- 【转】C语言中数组名和指针的区别
注:本文转自http://www.cnblogs.com/furaibo/archive/2010/03/19/1689710.html 魔幻数组名 请看程序(本文程序在WIN32平台下编译): #i ...
- 测试常用命令之awk篇
awk/gawk 1,内置变量 FILENAME:输入文件名称 FNR:当前数据文件中的数据行数 NF:数据文件中的字段总数 NR:已处理的输入数据行数目 FS:输入数据段分隔符 RS:输入数据行分隔 ...