SpringBoot自启动方式
SprinngBoot 有两种启动方式:
1、ApplicationRunner
默认情况
ApplicationRunner 优先级高于CommandLineRunner
可以通过@Order注解来调整优先级
package com.caicai.springboot.study.Runner; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; /* *添加的一个Bean注,因为需要被Spring 扫描到 * 默认情况 ApplicationRunner 执行优先级高于CommandRunner * */ //@Order(1) //调试执行优先级,数值越小,执行优先级越高 @Component // bean注解 @Slf4j public class SpringBootApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { log.info("This is ApplicationRunner "); } }
2、CommandLineRunner
package com.caicai.springboot.study.Runner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/*
* 添加Bean注解
* */
//@Order(1) //调试执行优先级,数值越小,执行优先级越高
@Component
@Slf4j
public class SpringBootCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
log.info("This is CommandLineRunner ");
}
}
参: