Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

java - How to get command Line arguments in spring boot?

@SpringBootApplication
public class CommandLinetoolApplication {

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

public static void main(String... argv) {
    SpringApplication.run(CommandLinetoolApplication.class, argv);
 }  
}

I am using eclipse so setting run configuration as
-Dspring-boot.run.arguments=--person.name=firstName

But when run my application,I am getting exception as "Could not resolve placeholder 'person.name' in value "${person.name}"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This code works just fine (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 );
    }
}

Command line:

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

The output:

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _    
( ( )\___ | '_ | '_| | '_ / _` |    
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...