如果需要访问SpringApplication.run(…)传入的参数,可以通过注解的形式注入
ApplicationArguments接口实例,该接口提供了一套访问String[]参数的方法和【不知道怎么翻译,原文是】as well as parsed option and non-option arguments:
import org.springframework.boot.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
@Component
public class MyBean {
@Autowired
public MyBean(ApplicationArguments args) {
boolean debug = args.containsOption("debug");
List<String> files = args.getNonOptionArgs();
// if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
}
}