Comment obtenir les arguments de ligne de commande dans le coffre de printemps?

Dnraj:
@SpringBootApplication
public class CommandLinetoolApplication {

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

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

J'utilise éclipse donc la mise en configuration d'exécution comme
-Dspring-boot.run.arguments = - = person.name prenom

Mais quand exécuter ma demande, je reçois comme exception « Impossible de résoudre l'espace réservé « person.name » de la valeur « $ {} person.name »

strelok:

Ce code fonctionne très bien (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 );
    }
}

Ligne de commande:

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

Le résultat:

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

Je suppose que tu aimes

Origine http://43.154.161.224:23101/article/api/json?id=184916&siteId=1
conseillé
Classement