Spring Boot Using the ApplicationRunner or CommandLineRunner

23.8 Using the ApplicationRunner or CommandLineRunner

如果你需要在SpringApplication启动时执行一些指定的代码,你能实现ApplicationRunnerCommandLineRunner接口。这两个接口工作方式相似并提供一个run方法,这个方法在SpringApplication.run()完成之前被调用。

CommandLineRunner接口提供的run方法使用字符串数组作为方法参数,ApplicationRunner接口提供的run方法使用ApplicationArguments对象作为方法。

import org.springframework.boot.*
import org.springframework.stereotype.*

@Component
public class MyBean implements CommandLineRunner {

    public void run(String... args) {
        // Do something...
    }

}

如果定义了多个CommandLineRunnerApplicationRunner,且这些bean需要按指定的顺序调用,你能采用额外实现org.springframework.core.Ordered或使用org.springframework.core.annotation.Order注解的方式来控制调用顺序。

猜你喜欢

转载自blog.csdn.net/quan20111992/article/details/80217280
今日推荐