启动成功后可以通过以下方法运行自己的初始代码:
- @PostConstruct注解
- ApplicationReadyEvent事件
- CommandLineRunner/ApplicationRunner接口
@Component
public class StartUpInit {
@Autowired
private SomeService service;
@PostConstruct
public void init(){
// ...
}
}
@Component
public class GeneralEventHandler {
@EventListener
public void handleApplicationReady(ApplicationReadyEvent event) {
log.info("The application is ready to service requests..");
}
}
Spring Boot提供了两个接口:CommandLineRunner、ApplicationRunner,用于启动应用时做特殊处理,这些代码会在SpringApplication的run()方法运行完成之前被执行。
通常用于应用启动前的特殊代码执行、特殊数据加载、垃圾数据清理、微服务的服务发现注册、系统启动成功后的通知等。相当于Spring的ApplicationListener、Servlet的ServletContextListener。
CommandLineRunner 和 ApplicationRunner 的区别是run()方法的参数不同。
(1)CommandLineRunner:参数是字符串数组
@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(CommandLineAppStartupRunner.class);
@Override
public void run(String... args) throws Exception {
logger.info("Application started with command-line arguments: {} . \n To kill this application, press Ctrl + C.", Arrays.toString(args));
}
}
(2)ApplicationRunner:参数被放入ApplicationArguments
通过getOptionNames()、getOptionValues()、getSourceArgs()获取参数
@Component
public class AppStartupRunner implements ApplicationRunner {
private static final Logger logger = LoggerFactory.getLogger(AppStartupRunner.class);
@Override
public void run(ApplicationArguments args) throws Exception {
logger.info("Your application started with option names : {}", args.getOptionNames());
}
}
也可以两个接口同时实现,但是没有必要。
@Component
public class StartupRunner implements CommandLineRunner, ApplicationRunner {
private static final Logger logger = LoggerFactory.getLogger(CommandLineAppStartupRunner.class);
@Override
public void run(String... args) throws Exception {
logger.info("Application started with command-line arguments: {} . \n To kill this application, press Ctrl + C.", Arrays.toString(args));
}
@Override
public void run(ApplicationArguments args) throws Exception {
logger.info("Your application started with option names : {}", args.getOptionNames());
}
}
也可以通过@Bean定义
@Configuration
public class RunnerConfig {
@Bean
public CommandLineRunner runner(){
return new CommandLineRunner() {
public void run(String... args){
System.out.println("CommandLineRunner run()");
}
};
}
}
(3)通过@Order设置执行顺序
@Component
@Order(3)
public class Runner1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Runner1 run()");
}
}
@Component
@Order(2)
public class Runner2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Runner2 run()");
}
}
@Component
@Order(1)
public class Runner3 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Runner3 run()");
}
}
(4)注入Bean
CommandLineRunner在被执行时,Spring内部已经启动完成,可以注入Spring的Bean。
@Component
public class StartupRunner implements CommandLineRunner {
@Autowired
private SampleService sampleService;
@Override
public void run(String... args) throws Exception {
sampleService.executeSample();
}
}
分享到:
相关推荐
赠送jar包:spring-boot-actuator-autoconfigure-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-actuator-autoconfigure-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-actuator-autoconfigure-2.3.12....
赠送jar包:seata-spring-boot-starter-1.3.0.jar; 赠送原API文档:seata-spring-boot-starter-1.3.0-javadoc.jar; 赠送源代码:seata-spring-boot-starter-1.3.0-sources.jar; 赠送Maven依赖信息文件:seata-...
赠送jar包:spring-boot-configuration-processor-2.6.3.jar; 赠送原API文档:spring-boot-configuration-processor-2.6.3-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-2.6.3-sources.jar; ...
赠送jar包:spring-boot-configuration-processor-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-configuration-processor-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-...
赠送jar包:druid-spring-boot-starter-1.1.9.jar; 赠送原API文档:druid-spring-boot-starter-1.1.9-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.1.9-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:mybatis-spring-boot-autoconfigure-1.3.2.jar; 赠送原API文档:mybatis-spring-boot-autoconfigure-1.3.2-javadoc.jar; 赠送源代码:mybatis-spring-boot-autoconfigure-1.3.2-sources.jar; 赠送...
赠送jar包:spring-boot-configuration-processor-2.5.6.jar; 赠送原API文档:spring-boot-configuration-processor-2.5.6-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-2.5.6-sources.jar; ...
赠送jar包:spring-boot-autoconfigure-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-autoconfigure-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-autoconfigure-2.3.12.RELEASE-sources.jar; 赠送...
赠送jar包:druid-spring-boot-starter-1.1.10.jar; 赠送原API文档:druid-spring-boot-starter-1.1.10-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.1.10-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:dynamic-datasource-spring-boot-starter-3.4.1.jar; 赠送原API文档:dynamic-datasource-spring-boot-starter-3.4.1-javadoc.jar; 赠送源代码:dynamic-datasource-spring-boot-starter-3.4.1-sources...
赠送jar包:nacos-config-spring-boot-autoconfigure-0.2.7.jar; 赠送原API文档:nacos-config-spring-boot-autoconfigure-0.2.7-javadoc.jar; 赠送源代码:nacos-config-spring-boot-autoconfigure-0.2.7-sources...
赠送jar包:dynamic-datasource-spring-boot-starter-3.4.1.jar; 赠送原API文档:dynamic-datasource-spring-boot-starter-3.4.1-javadoc.jar; 赠送源代码:dynamic-datasource-spring-boot-starter-3.4.1-sources...
spring-boot项目pom.xm中parent依赖文件spring-boot-starter-parent-1.5.13.RELEASE.pom
mybatis-spring-boot-starter-2.1.3.jarmybatis-spring-boot-starter-2.1.3.jarmybatis-spring-boot-starter-2.1.3.jar
赠送jar包:spring-boot-actuator-autoconfigure-2.5.5.jar; 赠送原API文档:spring-boot-actuator-autoconfigure-2.5.5-javadoc.jar; 赠送源代码:spring-boot-actuator-autoconfigure-2.5.5-sources.jar; 赠送...
赠送jar包:spring-boot-actuator-autoconfigure-2.2.0.RELEASE.jar; 赠送原API文档:spring-boot-actuator-autoconfigure-2.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-boot-actuator-autoconfigure-2.2.0....
druid-spring-boot-starter-1.2.8.jar
mybatis-spring-boot-starter-2.1.4.jarmybatis-spring-boot-starter-2.1.4.jar
activiti-spring-boot-starter-basic-6.0.0适配springboot2.1.2
spring-boot-starter-web-2.0.7.0.jar