个人笔记-服务启动加载及注解开发
首先介绍ApplicationRunner和CommandLineRunner接口,两个接口以相同的方式工作并提供单个run方法,该方法在SpringApplication.run(…)完成之前调用 。CommandLineRunner接口提供访问的应用程序的参数作为一个简单的字符串数组,
而ApplicationRunner使用了ApplicationArguments,ApplicationArguments 接口提供对原始String[]参数以及解析option 和non-option参数的访问。
1. 实现ApplicationRunner接口
想要实现在spring启动后自动执行自己编写的代码,分如下几步:
a. 自定义一个类
b. 此类要被spring管理
c. 实现ApplicationRunner
d. 重写run方法,实现自己的代码逻辑
如果要实现多步有序代码逻辑,可以在类上加注解@Order(value = 1)
代码如下:
第一个想要执行的启动类:
package com.weixiaoyi.runner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* @author :yuanLong Wei
* @date :Created in 2019/5/14 17:56
* @description:第一个启动运行类
* @modified By:
* @version: 1.0
*/
@Order(value = 1)
@Component
@Slf4j
public class MyFirstRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("first Runner -- 执行了");
}
}
第二个想要执行的启动类:
package com.weixiaoyi.runner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* @author :yuanLong Wei
* @date :Created in 2019/5/14 17:57
* @description:第二个启动运行类
* @modified By:
* @version: 1.0
*/
@Order(value = 2)
@Component
@Slf4j
public class MySecondRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("second Runner -- 执行了。。。");
}
}
效果如下:
2. 实现CommandLineRunner接口
与实现ApplicationRunner接口相似,只相差了所实现的接口不同,这里要实现CommandLineRunner接口。
两种方式接受参数方式不同。@Order对于两种情况同样有效
package com.weixiaoyi.runner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* @author :yuanLong Wei
* @date :Created in 2019/5/16 14:32
* @description:第三个启动运行类
* @modified By:
* @version: 1.0
*/
@Slf4j
@Component
@Order(3)
public class MyThiredRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
log.info("Thired Runner -- 执行了。。。");
}
}
效果如下:
3. 实现ApplicationListener接口
实现此接口要传入不同的泛型,此泛型为ApplicationEvent的子类,spring为我们定义好了很多这样的事件监听类,
传入不同的泛型可以达到在不同时期触发自己要实现的逻辑。
这里以ApplicationStartedEvent和ApplicationReadyEvent为例:
package com.weixiaoyi.listener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* @author :yuanLong Wei
* @date :Created in 2019-5-16 14:38:08
* @description:可以传入不通泛型 进行在spring启动的不同时期 进行执行
* @modified By:
* @version: 1.0
*/
@Slf4j
@Component
public class SpringStartListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
log.info("ApplicationStartedEvent listener -- 执行了");
}
}
package com.weixiaoyi.listener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* @author :yuanLong Wei
* @date :Created in 2019/5/16 14:42
* @description:
* @modified By:
* @version: 1.0
*/
@Slf4j
@Component
public class SpringReadyListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
log.info("ApplicationReadyEvent listener -- 执行了");
}
}
效果如下:
网上没有找到特别详细的所有类的介绍,这里就不引入链接了。
4. 实现注解开发
注解开发可以实现在方法级别上,不需要继承接口,也不需要重写方法:
package com.weixiaoyi.listener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* @author :yuanLong Wei
* @date :Created in 2019-5-16 15:06:12
* @description:注解使用
* @modified By:
* @version: 1.0
*/
@Component
@Slf4j
public class SpringStartAnnoListener {
@EventListener
public void listenSpringStarted(ApplicationStartedEvent restVo) {
log.info("ApplicationStartedEvent 注解实现执行了。。。");
}
}
也可以在注解上传入实现类的类型:@EventListener(ApplicationStartedEvent.class)
虽然是方法级别的监听,但所在的类也一定要被spring管理
效果如下: