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

时间:2019-04-26 13:13:18

标签: 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);
 }  
}

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

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

3 个答案:

答案 0 :(得分:1)

此代码可以正常工作(Spring Boot 2.1.4):

$ sudo service jenkins start
 Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details.


$ systemctl status jenkins.service

● jenkins.service - LSB: Jenkins Automation Server
   Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
   Active: failed (Result: exit-code) since Sun 2019-04-28 18:15:27 IST; 1s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 21448 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=1/FAILURE)
Apr 28 18:15:27 UTIBTSV02 systemd[1]: Starting LSB: Jenkins Automation Server...
Apr 28 18:15:27 UTIBTSV02 runuser[21454]: pam_unix(runuser:session): session opened for user jenkins by (uid=0)
Apr 28 18:15:27 UTIBTSV02 jenkins[21448]: Starting Jenkins bash: -c: line 0: unexpected EOF while looking for matching `"'
Apr 28 18:15:27 UTIBTSV02 jenkins[21448]: bash: -c: line 1: syntax error: unexpected end of file
Apr 28 18:15:27 UTIBTSV02 jenkins[21448]: [FAILED]
Apr 28 18:15:27 UTIBTSV02 systemd[1]: jenkins.service: control process exited, code=exited status=1
Apr 28 18:15:27 UTIBTSV02 systemd[1]: Failed to start LSB: Jenkins Automation Server.
Apr 28 18:15:27 UTIBTSV02 systemd[1]: Unit jenkins.service entered failed state.
Apr 28 18:15:27 UTIBTSV02 systemd[1]: jenkins.service failed.

命令行:

@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 );
    }
}

输出:

mvn spring-boot:run -Dspring-boot.run.arguments=--person.name=Test

答案 1 :(得分:0)

您需要在person.name=firstName

中添加配置属性application.properties

OR

实施接口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);
    }
}

答案 2 :(得分:0)

您需要将Eclipse虚拟机参数更改为 -Dperson.name = dhanraj

另一件事是没有必要在主类中添加私有字符串名称。 由于main方法是静态方法,因此您需要创建对象以访问名称 变量,最终新对象会为您提供空值,而不是您设置的 dhanraj 值。

因此,请在控制器或服务部分中使用此变量。