springboot 特定code 在SpringApplica
Using the ApplicationRunner or CommandLineRunner
If you need to run some specific code once the SpringApplication has started, you can implement the ApplicationRunner or CommandLineRunner interfaces. Both interfaces work in the same way and offer a single run method, which is called just before SpringApplication.run(…) completes.// 继承ApplicationRunner 或CommandLineRunner
The CommandLineRunner interfaces provides access to application arguments as a simple string array, whereas the ApplicationRunner uses the ApplicationArguments interface discussed earlier. The following example shows a CommandLineRunner with a run method://具体实例如下:
@Component
public class MyBean implements CommandLineRunner {
publicvoidrun(String... args) {
// Do something...
}
}
If several CommandLineRunner or ApplicationRunner beans are defined that must be called in a specific order, you can additionally implement the org.springframework.core.Ordered interface or use the org.springframework.core.annotation.Order annotation.(如果多个这样的类需要有顺序,实现接口org.springframework.core.Ordered 或者加注解org.springframework.core.annotation.Order)
这是init的类,对于有个“destroy”的,用到时另查