Saturday, 28 July 2018

Spring boot: Get the port of spring boot application

There are many ways to know about the port that spring boot application is running.
a.   From the log output
b.   From the ServletWebServerApplicationContext through its WebServer
c.   Using ApplicationListener<ServletWebServerInitializedEvent>

From the log output

Application.java
package com.sample.myApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
  SpringApplication.exit(applicationContext);
 }

}

Run above application, in the console log, you can able to see below messages.

INFO 2492 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
INFO 2492 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
INFO 2492 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
INFO 2492 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
INFO 2492 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

From the ServletWebServerApplicationContext through its WebServer

ServerDetails.java
package com.sample.myApp.model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class ServerDetails {
 public ServletWebServerApplicationContext getServer() {
  return server;
 }

 public void setServer(ServletWebServerApplicationContext server) {
  this.server = server;
 }

 @Autowired
 private ServletWebServerApplicationContext server;
 
 
}

Application.java
package com.sample.myApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.sample.myApp.model.ServerDetails;

@SpringBootApplication
public class Application {

 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  ServerDetails serverDetails = applicationContext.getBean(ServerDetails.class);

  int port = serverDetails.getServer().getWebServer().getPort();

  System.out.println("************************************");
  System.out.printf("Web server is running on %d\n", port);
  System.out.println("************************************");
  SpringApplication.exit(applicationContext);
 }

}


When you ran Application.java, you can able to see below messages in console.
************************************
Web server is running on 8080
************************************

Using ApplicationListener<ServletWebServerInitializedEvent>

PortListener.java
package com.sample.myApp.listeners;

import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class PortListener implements ApplicationListener<ServletWebServerInitializedEvent> {

 @Override
 public void onApplicationEvent(final ServletWebServerInitializedEvent event) {
  int port = event.getWebServer().getPort();

  System.out.println("************************************");
  System.out.printf("Web server is running on %d\n", port);
  System.out.println("************************************");
 }
}


Application.java
package com.sample.myApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  SpringApplication.exit(applicationContext);
 }

}

When you ran Application.java, you can able to see below messages in console.

************************************
Web server is running on 8080

************************************









Previous                                                 Next                                                 Home

No comments:

Post a Comment