SpringBoot官方文档翻译(三十六):访问应用程序参数

23.6 Accessing Application Arguments(访问应用程序参数)

1
2
If you need to access the application arguments that were passed to SpringApplication.run(… ), you can inject a org.springframework.boot.ApplicationArguments bean. The ApplicationArguments interface provides access to both the raw String[] arguments as well as parsed option and non-option arguments,     
as shown in the following example:

您可以通过注入一个org.springframework.boot.ApplicationArguments bean去获取访问通过SpringApplication.run(… )传递的参数。ApplicationArguments参数接口即提供了访问原生String[]参数的方式,也提供了访问解析option(可选)和non-option(非可选)参数。举个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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"]
}

}

1
2
3
Spring Boot also registers a CommandLinePropertySource with the    
Spring Environment. This lets you also inject single application
arguments by using the @Value annotation.

Spring Boot还向Spring环境注册了一个CommandLinePropertySource。这使您同样能够通过使用注解@Value注入单应用参数。

请为以下账号开通访问 http://qahome.dp/smsqueue/query 的权限
wbhz_zhengzhongguo
wbhz_178304746
wbhz_majing_fzlzh
wbhz_songchang
wbhz_ganjiali
wbhz_lushen
wbhz_fangjie

分享到