我这里的版本是2.1.8

package com.ysy.config;

import com.ysy.HelloDemo;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

/**
 * Created by Administrator on 2020/1/28 19:34
 */
@Configuration
public class webServerConfig {

   //方式1
    @EventListener(WebServerInitializedEvent.class)
    public void onWebServerReady(WebServerInitializedEvent event) {
        System.out.println("1.当前WebServer实现类为:"+event.getWebServer().getClass().getName());
        WebServerApplicationContext applicationContext = event.getApplicationContext();
        HelloDemo bean = applicationContext.getBean(HelloDemo.class);
        int port = applicationContext.getWebServer().getPort();
        String name = applicationContext.getWebServer().getClass().getName();
        System.out.println("获取bean对象:"+bean);
        System.out.println("1.容器端口"+port+",实现类-"+name);
    }
    /**
     * 在spring boot应用启动后回调
     * @param context
     * @return
     */
//方式2
    @Bean
    public ApplicationRunner runner(WebServerApplicationContext context) {
        return args -> {
            System.out.println("2.当前WebServer实现类为:"+context.getWebServer().getClass().getName());
        };
    }


    for 1.5.x  Spring Boot
//public class ServerConfig implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {
//    private int serverPort;;
//
//    public int getPort() {
//        return this.serverPort;
//    }
//
//    @Override
//    public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
//        this.serverPort = event.getEmbeddedServletContainer().getPort();
//    }
//}
}

E:\software\JDK1.8\jdk8\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:E:\software\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=51066:E:\software\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath E:\software\JDK1.8\jdk8\jre\lib\charsets.jar;E:\software\JDK1.8\jdk8\jre\lib\deploy.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\access-bridge-64.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\cldrdata.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\dnsns.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\jaccess.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\jfxrt.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\localedata.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\nashorn.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\sunec.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\sunjce_provider.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\sunmscapi.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\sunpkcs11.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\zipfs.jar;E:\software\JDK1.8\jdk8\jre\lib\javaws.jar;E:\software\JDK1.8\jdk8\jre\lib\jce.jar;E:\software\JDK1.8\jdk8\jre\lib\jfr.jar;E:\software\JDK1.8\jdk8\jre\lib\jfxswt.jar;E:\software\JDK1.8\jdk8\jre\lib\jsse.jar;E:\software\JDK1.8\jdk8\jre\lib\management-agent.jar;E:\software\JDK1.8\jdk8\jre\lib\plugin.jar;E:\software\JDK1.8\jdk8\jre\lib\resources.jar;E:\software\JDK1.8\jdk8\jre\lib\rt.jar;E:\project\springboot_learning\springboot01\target\classes;E:\response\org\springframework\boot\spring-boot-starter-undertow\2.1.8.RELEASE\spring-boot-starter-undertow-2.1.8.RELEASE.jar;E:\response\io\undertow\undertow-core\2.0.26.Final\undertow-core-2.0.26.Final.jar;E:\response\org\jboss\logging\jboss-logging\3.3.3.Final\jboss-logging-3.3.3.Final.jar;E:\response\org\jboss\xnio\xnio-api\3.3.8.Final\xnio-api-3.3.8.Final.jar;E:\response\org\jboss\xnio\xnio-nio\3.3.8.Final\xnio-nio-3.3.8.Final.jar;E:\response\io\undertow\undertow-servlet\2.0.26.Final\undertow-servlet-2.0.26.Final.jar;E:\response\org\jboss\spec\javax\annotation\jboss-annotations-api_1.2_spec\1.0.2.Final\jboss-annotations-api_1.2_spec-1.0.2.Final.jar;E:\response\io\undertow\undertow-websockets-jsr\2.0.26.Final\undertow-websockets-jsr-2.0.26.Final.jar;E:\response\org\jboss\spec\javax\websocket\jboss-websocket-api_1.1_spec\1.1.4.Final\jboss-websocket-api_1.1_spec-1.1.4.Final.jar;E:\response\javax\servlet\javax.servlet-api\4.0.1\javax.servlet-api-4.0.1.jar;E:\response\org\glassfish\javax.el\3.0.0\javax.el-3.0.0.jar;E:\response\org\springframework\boot\spring-boot-starter\2.1.8.RELEASE\spring-boot-starter-2.1.8.RELEASE.jar;E:\response\org\springframework\boot\spring-boot\2.1.8.RELEASE\spring-boot-2.1.8.RELEASE.jar;E:\response\org\springframework\spring-context\5.1.9.RELEASE\spring-context-5.1.9.RELEASE.jar;E:\response\org\springframework\spring-aop\5.1.9.RELEASE\spring-aop-5.1.9.RELEASE.jar;E:\response\org\springframework\spring-expression\5.1.9.RELEASE\spring-expression-5.1.9.RELEASE.jar;E:\response\org\springframework\boot\spring-boot-autoconfigure\2.1.8.RELEASE\spring-boot-autoconfigure-2.1.8.RELEASE.jar;E:\response\org\springframework\boot\spring-boot-starter-logging\2.1.8.RELEASE\spring-boot-starter-logging-2.1.8.RELEASE.jar;E:\response\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;E:\response\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;E:\response\org\apache\logging\log4j\log4j-to-slf4j\2.11.2\log4j-to-slf4j-2.11.2.jar;E:\response\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;E:\response\org\slf4j\jul-to-slf4j\1.7.28\jul-to-slf4j-1.7.28.jar;E:\response\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;E:\response\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;E:\response\org\slf4j\slf4j-api\1.7.28\slf4j-api-1.7.28.jar;E:\response\org\springframework\spring-core\5.1.9.RELEASE\spring-core-5.1.9.RELEASE.jar;E:\response\org\springframework\spring-jcl\5.1.9.RELEASE\spring-jcl-5.1.9.RELEASE.jar;E:\response\org\springframework\boot\spring-boot-starter-webflux\2.1.8.RELEASE\spring-boot-starter-webflux-2.1.8.RELEASE.jar;E:\response\org\springframework\boot\spring-boot-starter-json\2.1.8.RELEASE\spring-boot-starter-json-2.1.8.RELEASE.jar;E:\response\com\fasterxml\jackson\core\jackson-databind\2.9.9.3\jackson-databind-2.9.9.3.jar;E:\response\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;E:\response\com\fasterxml\jackson\core\jackson-core\2.9.9\jackson-core-2.9.9.jar;E:\response\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.9\jackson-datatype-jdk8-2.9.9.jar;E:\response\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.9\jackson-datatype-jsr310-2.9.9.jar;E:\response\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.9\jackson-module-parameter-names-2.9.9.jar;E:\response\org\springframework\boot\spring-boot-starter-reactor-netty\2.1.8.RELEASE\spring-boot-starter-reactor-netty-2.1.8.RELEASE.jar;E:\response\io\projectreactor\netty\reactor-netty\0.8.11.RELEASE\reactor-netty-0.8.11.RELEASE.jar;E:\response\io\netty\netty-codec-http\4.1.39.Final\netty-codec-http-4.1.39.Final.jar;E:\response\io\netty\netty-common\4.1.39.Final\netty-common-4.1.39.Final.jar;E:\response\io\netty\netty-buffer\4.1.39.Final\netty-buffer-4.1.39.Final.jar;E:\response\io\netty\netty-transport\4.1.39.Final\netty-transport-4.1.39.Final.jar;E:\response\io\netty\netty-resolver\4.1.39.Final\netty-resolver-4.1.39.Final.jar;E:\response\io\netty\netty-codec\4.1.39.Final\netty-codec-4.1.39.Final.jar;E:\response\io\netty\netty-codec-http2\4.1.39.Final\netty-codec-http2-4.1.39.Final.jar;E:\response\io\netty\netty-handler\4.1.39.Final\netty-handler-4.1.39.Final.jar;E:\response\io\netty\netty-handler-proxy\4.1.39.Final\netty-handler-proxy-4.1.39.Final.jar;E:\response\io\netty\netty-codec-socks\4.1.39.Final\netty-codec-socks-4.1.39.Final.jar;E:\response\io\netty\netty-transport-native-epoll\4.1.39.Final\netty-transport-native-epoll-4.1.39.Final-linux-x86_64.jar;E:\response\io\netty\netty-transport-native-unix-common\4.1.39.Final\netty-transport-native-unix-common-4.1.39.Final.jar;E:\response\org\hibernate\validator\hibernate-validator\6.0.17.Final\hibernate-validator-6.0.17.Final.jar;E:\response\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;E:\response\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;E:\response\org\springframework\spring-web\5.1.9.RELEASE\spring-web-5.1.9.RELEASE.jar;E:\response\org\springframework\spring-beans\5.1.9.RELEASE\spring-beans-5.1.9.RELEASE.jar;E:\response\org\springframework\spring-webflux\5.1.9.RELEASE\spring-webflux-5.1.9.RELEASE.jar;E:\response\io\projectreactor\reactor-core\3.2.12.RELEASE\reactor-core-3.2.12.RELEASE.jar;E:\response\org\reactivestreams\reactive-streams\1.0.3\reactive-streams-1.0.3.jar;E:\response\org\synchronoss\cloud\nio-multipart-parser\1.1.0\nio-multipart-parser-1.1.0.jar;E:\response\org\synchronoss\cloud\nio-stream-storage\1.1.3\nio-stream-storage-1.1.3.jar com.ysy.HelloDemo

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2020-01-28 19:46:54.103  INFO 5276 --- [           main] com.ysy.HelloDemo                        : Starting HelloDemo on Win7-2020USJWDB with PID 5276 (E:\project\springboot_learning\springboot01\target\classes started by Administrator in E:\project\springboot_learning\springboot01)
2020-01-28 19:46:54.109  INFO 5276 --- [           main] com.ysy.HelloDemo                        : No active profile set, falling back to default profiles: default
2020-01-28 19:46:57.356  INFO 5276 --- [           main] org.xnio                                 : XNIO version 3.3.8.Final
2020-01-28 19:46:57.377  INFO 5276 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.3.8.Final
2020-01-28 19:46:57.489  INFO 5276 --- [           main] o.s.b.w.e.undertow.UndertowWebServer     : Undertow started on port(s) 8080 (http)
1.当前WebServer实现类为:org.springframework.boot.web.embedded.undertow.UndertowWebServer
获取bean对象:com.ysy.HelloDemo$$EnhancerBySpringCGLIB$$d8c48f55@3fabf088
1.容器端口8080,实现类-org.springframework.boot.web.embedded.undertow.UndertowWebServer
2020-01-28 19:46:57.497  INFO 5276 --- [           main] com.ysy.HelloDemo                        : Started HelloDemo in 5.056 seconds (JVM running for 6.906)
2.当前WebServer实现类为:org.springframework.boot.web.embedded.undertow.UndertowWebServer

Process finished with exit code -1

2个方法都可以兼容webservlet和Reactive;
其中WebServerInitializedEvent的兼容性更强(方便扩展非web)

各自继承关系如下:



更多推荐

Spring Boot获取启动端口2种方式