如何在 Spring Boot 中获取命令行参数?

Dha*_*raj 10 java spring spring-boot

@SpringBootApplication
public class CommandLinetoolApplication {

@Value("${person.name}")
private String name;

public static void main(String... argv) {
    SpringApplication.run(CommandLinetoolApplication.class, argv);
 }  
}
Run Code Online (Sandbox Code Playgroud)

我正在使用 Eclipse,因此将运行配置设置为
-Dspring-boot.run.arguments=--person.name=firstName

但是当运行我的应用程序时,我收到异常,因为“无法解析值“${person.name}”中的占位符“person.name”

Str*_*lok 21

这段代码工作得很好(Spring Boot 2.1.4):

@SpringBootApplication
public class DemoApplication implements ApplicationRunner
{

    @Value("${person.name}")
    private String name;

    public static void main( String[] args )
    {
        SpringApplication.run( DemoApplication.class, args );
    }

    @Override
    public void run( ApplicationArguments args ) throws Exception
    {
        System.out.println( "Name: " + name );
    }
}
Run Code Online (Sandbox Code Playgroud)

命令行:

mvn spring-boot:run -Dspring-boot.run.arguments=--person.name=Test
Run Code Online (Sandbox Code Playgroud)

输出:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2019-04-28 22:51:09.741  INFO 73751 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on xxx-MacBook-Pro.local with PID 73751 (/Users/strelok/code/demo-sb/target/classes started by strelok in /Users/strelok/code/demo-sb)
2019-04-28 22:51:09.745  INFO 73751 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2019-04-28 22:51:10.943  INFO 73751 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 16.746 seconds (JVM running for 23.386)
Name: Test
Run Code Online (Sandbox Code Playgroud)


abj*_*305 5

您需要person.name=firstName在您的application.properties

或者

实现接口ApplicationRunner并覆盖其run方法(读取命令行参数的正确方法)

例子:

@SpringBootApplication
public class Application implements ApplicationRunner {

    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String... args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("Application started with command-line arguments: {}", Arrays.toString(args.getSourceArgs()));
        logger.info("NonOptionArgs: {}", args.getNonOptionArgs());
        logger.info("OptionNames: {}", args.getOptionNames());

        for (String name : args.getOptionNames()){
            logger.info("arg-" + name + "=" + args.getOptionValues(name));
        }

        boolean containsOption = args.containsOption("person.name");
        logger.info("Contains person.name: " + containsOption);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 您仅从 https://memorynotfound.com/spring-boot-passing-command-line-arguments-example/ 复制了此代码 (2认同)
  • `@Value("${person.name}")` 不会读取命令行。它从 application.properties 读取 (2认同)