23.7 Accessing application arguments

如果需要获取传递给SpringApplication.run(…)的应用参数,你可以注入一个org.springframework.boot.ApplicationArguments类型的bean。ApplicationArguments接口即提供对原始String[]参数的访问,也提供对解析成option和non-option参数的访问:

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"]
    }

}

(原文)Spring Boot will also register a CommandLinePropertySource with the Spring Environment. This allows you to also inject single application arguments using the @Value annotation.

Spring Boot会将CommandLinePropertySource(abstarct class,它有一个实现类SimpleCommandLinePropertySource)加入到Spring ApplicationContextEnvironment中,因此,只要注册了PropertyResolver(Spring Boot会自动注册PropertySourcesPropertyResolver),就允许你使用@Value注解注入单个的应用参数,具体用法可参见(CommandLinePropertySource

@Component
 public class MyComponent {
     // "--debug" 注入的是"",因为没有value
     @Value("${debug}")
     private String myProperty1;

     // "--debug=tt" 注入的是"tt"
     @Value("${debug}")
     private String myProperty2;

     // "/path/to/file1 /path/to/file2" 注入的是"/path/to/file1 ,/path/to/file2"
     //所有的 non-option参数的name都是 nonOptionArgs
     @Value("${nonOptionArgs}")
     private String myProperty3;

     public void getMyProperty1() {
         return this.myProperty1;
     }

     public void getMyProperty2() {
         return this.myProperty2;
     }

     public void getMyProperty3() {
         return this.myProperty3;
     }


     // ...
 }

当然也可以用注入Environment的方法

@Configuration
 public class AppConfig {

     @Inject Environment env;

     @Bean
     public void DataSource dataSource() {
         MyVendorDataSource dataSource = new MyVendorDataSource();
         dataSource.setHostname(env.getProperty("db.hostname", "localhost"));
         dataSource.setUsername(env.getRequiredProperty("db.username"));
         dataSource.setPassword(env.getRequiredProperty("db.password"));
         // ...
         return dataSource;
     }
 }

当然如果是传统的Spring-based 应用肯定就不会自动给容器的Environment添加CommandLinePropertySource(类的字面意思,来自命令行的属性源,),需要手动加入:

 public static void main(String[] args) {
     //CommandLinePropertySource 是抽象类,"..."代替具体初始化过程
     CommandLinePropertySource clps = ...;
     AnnotationConfigApplicationContext ctx = 
         new AnnotationConfigApplicationContext();
     //将来自CommandLine的属性作为首先检索的属性addFirst
     ctx.getEnvironment().getPropertySources().addFirst(clps);
     ctx.register(AppConfig.class);
     ctx.refresh();
 }

option和non-option参数

Working with option arguments

option参数是指带有"--"前缀的参数,形如:

--optName[=optValue]

它可以有特定的值(may or may not specify a value),如果有特定的值,那么参数名和值之间必须用"="隔开且中间不能有空格(If a value is specified, the name and value must be separated without spaces by an equals sign ("="))

符合规范的值如下:

-foo
 --foo=bar
 --foo="bar then baz"
 --foo=bar,baz,biz

不符合规范的值:

  -foo
 --foo bar
 --foo = bar
 --foo=bar --foo=baz --foo=biz

Working with non-option arguments

任何没有"--"前缀的参数就是non-option arguments

results matching ""

    No results matching ""