春季启动webapp准备就绪后自动启动浏览器 [英] Launch browser automatically after spring-boot webapp is ready

查看:72
本文介绍了春季启动webapp准备就绪后自动启动浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在启动spring boot应用程序后自动启动浏览器.是否有任何侦听器方法回调来检查webapp是否已部署并准备好处理请求,以便在加载浏览器时,用户看到索引页并可以开始与Webapp进行交互?

How do I launch a browser automatically after starting the spring boot application.Is there any listener method callback to check if the webapp has been deployed and is ready to serve the requests,so that when the browser is loaded , the user sees the index page and can start interacting with the webapp?

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    // launch browser on localhost 
}

推荐答案

以下代码对我有用:

@EventListener({ApplicationReadyEvent.class})
void applicationReadyEvent() {
    System.out.println("Application started ... launching browser now");
    browse("www.google.com");
}

public static void browse(String url) {
    if(Desktop.isDesktopSupported()){
        Desktop desktop = Desktop.getDesktop();
        try {
            desktop.browse(new URI(url));
        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
        }
    }else{
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这篇关于春季启动webapp准备就绪后自动启动浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆