【Java】【Spring】SpringBoot中添加监听器

添加一个监听器,在项目完全启动后,打印项目地址


@Component
public class ContextListener implements ApplicationListener {

    @Override
    @SneakyThrows
    public void onApplicationEvent(ApplicationReadyEvent event) {
        String host = InetAddress.getLocalHost().getHostAddress();
        TomcatServletWebServerFactory factory = (TomcatServletWebServerFactory) event.getApplicationContext().getBean("tomcatServletWebServerFactory");
        int port = factory.getPort();
        String path = factory.getContextPath();
        String url = "http://" + host + ":" + port + path;
        Console.print(ContextListener.class, "项目启动成功 " + url);
    }
}  

你可能感兴趣的:(java)