在项目开发中某些场景有必要要用到发动项目后当即履行办法的功能,如咱们需要去初始化数据到
redis
缓存,或者发动后读取相应的字典配置等,这篇文章首要聊聊完成当即履行的几种办法。
一、CommandLineRunner
和ApplicationRunner
这两者的完成办法相同,都是去继承相应的接口然后重写run
办法即可,也都是SpringBoot
结构所供给应咱们的接口,也是项目中最常用的,比较灵敏,有多个CommandLineRunner
或ApplicationRunner
完成类时能够经过@Order
注解或完成Ordered
接口重写getOrder
办法完成按指定次序履行
1. CommandLineRunner
的完成
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunnerImpl办法履行");
}
}
2. ApplicationRunner
的完成
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner办法履行");
}
3. 多个CommandLineRunner
或ApplicationRunner
完成类时指定次序履行
经过@Order
注解指定,数字越小越先履行
@Component
@Order(1) //经过order注解指定
public class ApplicationRunnerImpl implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner办法履行");
}
}
经过完成Ordered
接口并重写getOrder
办法完成,数字越小越先履行
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
@Component
public class ApplicationRunnerImpl implements ApplicationRunner, Ordered {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner办法履行");
}
@Override
public int getOrder() {
return 1;
}
}
-
CommandLineRunner
或ApplicationRunner
的不同
这两者的不同其实便是run
办法中所接收的参数类型不同,CommandLineRunner
是对咱们发动jar
包时所传参数不进行处理,原样返回String
类型给你,ApplicationRunner
是封装成了ApplicationArguments
参数,经过这个类型能够更便利的判断某些参数是否存在之类的。
二、JDK
所供给的@PostConstruct
@PostConstruct
是JDK
所供给的注解,使用该注解的办法会在服务器加载Servlet
的时候运转。也能够在一个类中写多个办法并增加这个注解。
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class PostConstructTest {
@PostConstruct
public void start() {
System.out.println("@PostConstruct办法履行");
}
@PostConstruct
public void start01() {
System.out.println("@PostConstruct1111办法履行");
}
}
三、其他办法(不常用)
1. ApplicationContextAware
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextAwareImpl implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAwareImpl办法履行");
}
}
2. ApplicationListener
(会履行屡次)
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class ApplicationListenerImpl implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("ApplicationListenerImpl办法履行");
}
}
3. InitializingBean
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class InitializingBeanImpl implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBeanImpl办法履行");
}
}
这些办法也都能够完成在发动项目后当即履行,但是不太常用。
以上便是在
SpringBoot
项目中完成发动项目后当即履行办法的几种办法,如果都我们有协助,记得点赞
、保藏
哦,谢谢!