3

In our environment, the port the server listens is always specified by a command line parameter:

java -jar myweb.jar --server.port=1024.

How can we get this value? In my AppSiteController.java I tried to use:

@Value("${server.port}")
private static String serverPort;

public static void main(String[] args) {
    System.out.println(serverPort);
    SpringApplication.run(AppSiteController.class, args);
}

Which returns null when the value is correctly specified on the command line.

Thanks.

user55305
  • 31
  • 3
  • 1
    your code is correct. only problem is how you are passing the argument. Check out the answer from Essex Boy. – pvpkiran Jun 28 '18 at 09:29

4 Answers4

1

For Spring Boot 2.x, you can override system properties like below:

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

By default,Spring Boot converts command-line arguments to properties and adds them as environment variables.

You can access command line argument from application's main method as:

@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.

mukesh210
  • 2,792
  • 2
  • 19
  • 41
  • Your suggestion worked 100%. My working code in `main()` is this: `for(String arg:args) { if (arg.startsWith("---server.port=")) System.out.println(arg.substring(15));}` However, it is not as elegant as `@Value` which doesn't work for me. – user55305 Jun 28 '18 at 11:21
  • I think, it should work for your code. Did you try with @Value annotation? – mukesh210 Jun 28 '18 at 11:45
1

Pass it as a system parameter to the jvm

java -Dserver.port=8080 -jar spring-boot.jar 

All java system parameters are added to the spring environment

Essex Boy
  • 7,565
  • 2
  • 21
  • 24
  • Your command did not have an effect; `java -jar spring-boot.jar --server.port=8080` has an effect, but it produces 'null' in the said `main()` ... – user55305 Jun 28 '18 at 11:17
  • @user55305 sorry I put the java parameter in the wrong place, needs to come after the java command, have corrected answer – Essex Boy Jun 28 '18 at 12:47
0

rather than using

@Value("${server.port}")
private static String serverPort;

consider using below in springboot application :

@Bean
    public String value(@Value("#{server.port}")String value){
        return value;
    }

And then access value as

SpringApplication.run(AppSiteController.class, args).getBean("value");

and pass in command line

-Dserver.port=...
Ashish Patil
  • 4,428
  • 1
  • 15
  • 36
0

After much research, I found that it is because Spring boot can't inject value into static variables. The solution is here:

Spring Boot @Value Properties

This code:

@Value("${server.port}")
private static String serverPort;

Should be this:

private static String serverPort;
@Value("${server.port}")
public void setPort(String value) {
    serverPort = value;
}
user55305
  • 31
  • 3
  • there's not point in doing this, the spring class is a singleton anyway, don't use the static, just have a class that's only used to wire values. – Essex Boy Jun 28 '18 at 13:33