springboot启动完成打印访问地址

import lombok.extern.log4j.Log4j;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.boot.context.embedded.EmbeddedServletContainer;

import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;

import org.springframework.boot.web.servlet.ServletComponentScan;

import org.springframework.boot.web.support.SpringBootServletInitializer;

import org.springframework.cache.annotation.EnableCaching;

import org.springframework.context.ApplicationListener;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.scheduling.annotation.EnableScheduling;

import java.net.InetAddress;

import java.util.Iterator;

import java.util.Properties;

import java.util.TimeZone;

/**

* main

*

* @author Administrator

*

*/

@SpringBootApplication

@ServletComponentScan

@EnableCaching

@EnableScheduling

@Log4j

public class MainApplicationextends SpringBootServletInitializerimplements ApplicationListener {

static EmbeddedServletContainerInitializedEventes;

  public static StringhostUrl="";

  public static void main(String[] args) {

TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");

      TimeZone.setDefault(tz);

      Properties props = System.getProperties();

      Iterator iter = props.keySet().iterator();

      while (iter.hasNext()) {

String key = (String) iter.next();

        log.debug(key +" = " + props.get(key));

      }

ConfigurableApplicationContext app = SpringApplication.run(MainApplication.class, args);

      EmbeddedServletContainer cont =es.getEmbeddedServletContainer();

      log.info("getApplicationName=" + app.getApplicationName());

      log.info("启动完成,port=" +es.getEmbeddedServletContainer().getPort());

      String host =null;

      try {

host = InetAddress.getLocalHost().getHostAddress();

      }catch (Exception e) {

}

hostUrl="http://" + host +":" + cont.getPort() +"" + app.getApplicationName();

      log.info(hostUrl);

  }

@Override

  protected SpringApplicationBuilderconfigure(SpringApplicationBuilder application) {

return application.sources(MainApplication.class);

  }

@Override

  public void onApplicationEvent(EmbeddedServletContainerInitializedEvent arg0) {

es = arg0;

  }

}

你可能感兴趣的:(springboot启动完成打印访问地址)