SpringBoot#应用启动后执行某些逻辑

// 方式1
@Component
public class WhenStartupA implements InitializingBean {
 
    @Override
    public void afterPropertiesSet() throws Exception {
        // ....
    }
}

// 方式2
@Component
@Order(value = 2)
public class WhenStartupB implements ApplicationRunner {
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // ....
    }
}

// 方式3
@Component
@Order(value = 1)
public class WhenStartupC implements CommandLineRunner {
 
    @Override
    public void run(String... args) throws Exception{
        // ....
    }
}


// 因为WhenStartupC的Order=1 小于WhenStartupB的Order=2,因此WhenStartupC会先执行

然后就是要熟悉springboot的启动流程

猜你喜欢

转载自www.cnblogs.com/luohaonan/p/11582500.html