0

What I am trying to do is, open home page link in a browser once my spring boot application started.

I have two profiles local & prod. Using local for now.

I am trying to read server address from properties file as @Value("${server.address}") private static String serverAddress;

but don't understand what's going wrong.

@SpringBootApplication
public class WebWalletApplication {

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

@Value("${server.address}")
private static String serverAddress;

public static void main(String[] args) throws URISyntaxException {
    SpringApplication.run(WebWalletApplication.class, args);
    openHomePage();
}

private static void openHomePage() throws URISyntaxException {
    System.out.println("serverAddress: " + serverAddress);
    String url = "https://" + serverAddress + ":" + serverPort + "/wallet/secure/home";
    URI homepage = new URI(url);

    if (Desktop.isDesktopSupported()) {
        Desktop desktop = Desktop.getDesktop();
        try {
            desktop.browse(homepage);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}
  1. application.properties

    spring.profiles.active=local

  2. application-local.properties

    server.address=192.168.1.79 server.port=8084

Swapnil Patil
  • 613
  • 6
  • 23

2 Answers2

1

Instead of injecting values into static fields and making the openHomePage() method static, make your class implement CommandLineRunner and implement the run() method to call openHomePage():

@SpringBootApplication
public class WebWalletApplication implements CommandLineRunner {

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

    @Value("${server.address}")
    private String serverAddress;

    public static void main(String[] args) {
        SpringApplication.run(WebWalletApplication.class, args);
    }

    @Override
    public void run(String... args) {
        try {
            openHomePage();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    private void openHomePage() throws URISyntaxException {
        System.out.println("serverAddress: " + serverAddress);
        String url = "https://" + serverAddress + ":" + serverPort + "/wallet/secure/home";
        URI homepage = new URI(url);

        if (Desktop.isDesktopSupported()) {
            Desktop desktop = Desktop.getDesktop();
            try {
                desktop.browse(homepage);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • thanks...working fine now But still I don't understand why it was happening... can you explain a bit... – Swapnil Patil Aug 28 '19 at 11:13
  • 1
    Injecting values with the `@Value` annotation only works on Spring beans. But you are calling `openHomePage()` directly in the `main` method, and Spring did not have a chance at that point to inject the values in the static members of the class. You don't have an instance of your class in that case which is treated as a Spring bean. – Jesper Aug 28 '19 at 11:25
1

@Value doesn't work with the static variables. To fix that either you can create a setter method and annotate with the @Value as shown below.

Example:

public static String somefield;

@Value("${property.value}")
public void setSomeField(String value) {
  somefield = value;
}
Rajesh
  • 4,273
  • 1
  • 32
  • 33
  • True, but if this is the answer, the question should be marked as duplicate of [this](https://stackoverflow.com/questions/7253694/spring-how-to-inject-a-value-to-static-field) and not be answered. – Ivar Aug 28 '19 at 11:21