springboot 特定code 在SpringApplica

2018-12-25  本文已影响0人  zz云飞扬

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”的,用到时另查

上一篇下一篇

猜你喜欢

热点阅读