I am currently working on a Springboot application and I need to access the current server port. The ports are being assigned randomly since i have defined server.port = 0
in my application.properties
.
I have seen multiple threads about this and they all point to adding:
@Value("${local.server.port}")
private int port;
However booting up the application prompts me with Could not resolve placeholder 'local.server.port' in string value "${local.server.port}"
To clarify, I am not setting up tests. I am using Springboot 1.5.7. Am I missing something? I'm trying to run the following simple example:
@SpringBootApplication
public class Main {
@Value("${local.server.port}")
private int port;
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@PostConstruct
public void printsomething() {
System.out.println("PORT " + this.port);
}
}