spring_reference/IV. Spring Boot features/22.6. Using the CommandLine...

16 lines
703 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 22.6. 命令行启动器
如果你想获取原始的命令行参数或一旦SpringApplication启动你需要运行一些特定的代码你可以实现CommandLineRunner接口。在所有实现该接口的Spring beans上将调用run(String… args)方法。
```java
import org.springframework.boot.*
import org.springframework.stereotype.*
@Component
public class MyBean implements CommandLineRunner {
public void run(String... args) {
// Do something...
}
}
```
如果一些CommandLineRunner beans被定义必须以特定的次序调用你可以额外实现org.springframework.core.Ordered接口或使用org.springframework.core.annotation.Order注解。