CommandLineArgumentsinSpringBoot

编程

We can use command-line arguments to configure our application, override application properties or pass custom arguments.

2. Maven Command-Line Arguments

First, let"s see how we can pass arguments while running our application using Maven Plugin.

Later, we"ll see how to access the arguments in our code.

2.1. Spring Boot 1.x

For Spring Boot 1.x, we can pass the arguments to our application using -Drun.arguments:

1

mvn spring-boot:run -Drun.arguments=--customArgument=custom

We can also pass multiple parameters to our app:

1

mvn spring-boot:run -Drun.arguments=--spring.main.banner-mode=off,--customArgument=custom

Note that:

  • Arguments should be comma separated
  • Each argument should be prefixed with —
  • We can also pass configuration properties, like spring.main.banner-mode shown in the example above

2.2. Spring Boot 2.x

For Spring Boot 2.x, we can pass the arguments using -Dspring-boot.run.arguments:

1

mvn spring-boot:run -Dspring-boot.run.arguments=--spring.main.banner-mode=off,--customArgument=custom

3. Gradle Command-Line Arguments

Next, let"s discover how to pass arguments while running our application using Gradle Plugin.

We"ll need to configure our bootRun task in build.gradle file:

1

2

3

4

5

bootRun {

    

if

(project.hasProperty(

"args"

)) {

        

args project.args.split(

","

)

    

}

}

Now, we can pass the command-line arguments as follows:

1

.

/gradlew

bootRun -Pargs=--spring.main.banner-mode=off,--customArgument=custom

4. Overriding System Properties

Other than passing custom arguments, we can also override system properties.

For example, here"s our application.properties file:

1

2

server.port=8081

spring.application.name=SampleApp

To override the server.port value, we need to pass the new value in the following manner (for Spring Boot 1.x):

1

mvn spring-boot:run -Drun.arguments=--server.port=8085

Similarly for Spring Boot 2.x:

1

mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085

Note that:

  • Spring Boot converts command-line arguments to properties and adds them as environment variables
  • We can use short command-line arguments –port=8085 instead of –server.port=8085 by using a placeholder in our application.properties:

    1

    server.port=${port:8080}

  • Command-line arguments take precedence over application.properties values

If needed, we can stop our application from converting command-line arguments to properties:

1

2

3

4

5

6

7

8

@SpringBootApplication

public

class

Application

extends

SpringBootServletInitializer {

    

public

static

void

main(String[] args) {

        

SpringApplication application =

new

SpringApplication(Application.

class

);

        

application.setAddCommandLineProperties(

false

);

        

application.run(args);

    

}

}

5. Accessing Command-Line Arguments

Let"s see how we can access the command-line arguments from our application"s main() method:

1

2

3

4

5

6

7

8

9

@SpringBootApplication

public

class

Application

extends

SpringBootServletInitializer {

    

public

static

void

main(String[] args) {

        

for

(String arg:args) {

            

System.out.println(arg);

        

}

        

SpringApplication.run(Application.

class

, args);

    

}

}

This will print the arguments we passed to our application from command-line, but we could also use them later in our application.

6. Passing Command-Line Arguments to the SpringBootTest

With the release of Spring Boot 2.2, we gained the possibility to inject command-line arguments during testing using @SpringBootTest and its args attribute:

1

2

3

4

5

6

7

8

@SpringBootTest

(args =

"--spring.main.banner-mode=off"

)

public

class

ApplicationTest {

 

    

@Test

    

public

void

whenUsingSpringBootTestArgs_thenCommandLineArgSet(

@Autowired

Environment env) {

        

Assertions.assertThat(env.getProperty(

"spring.main.banner-mode"

)).isEqualTo(

"off"

);

    

}

}

7. Conclusion

In this article, we learned how to pass arguments to our Spring Boot application from command-line, and how to do it using both Maven and Gradle.

We"ve also shown how you can access those arguments from your code, in order to configure your application.

以上是 CommandLineArgumentsinSpringBoot 的全部内容, 来源链接: utcz.com/z/513032.html

回到顶部