在spring boot webapp就绪后自动启动浏览器

在spring boot webapp就绪后自动启动浏览器,spring,spring-boot,Spring,Spring Boot,如何在启动spring boot应用程序后自动启动浏览器。是否有任何侦听器方法回调来检查webapp是否已部署并准备好为请求提供服务,以便在加载浏览器时,用户可以看到索引页并开始与webapp交互 public static void main(String[] args) { SpringApplication.run(Application.class, args); // launch browser on localhost } 您可以通过一些java代码来实现。我不

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

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

您可以通过一些java代码来实现。我不确定弹簧靴是否有现成的东西

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Browser {
    public static void main(String[] args) {
        String url = "http://www.google.com";

        if(Desktop.isDesktopSupported()){
            Desktop desktop = Desktop.getDesktop();
            try {
                desktop.browse(new URI(url));
            } catch (IOException | URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else{
            Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("xdg-open " + url);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

以下代码适用于我:

@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();
        }
    }
}

我最近一直在尝试让这项工作自己,我知道这是一段时间以来,因为这个问题被问到,但我的工作(非常基本/简单)的解决方案如下所示。这是一个起点,任何人想要得到这个工作,重构需要在您的应用程序

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

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        openHomePage();
    }

    private static void openHomePage() {
        try {
            URI homepage = new URI("http://localhost:8080/");
            Desktop.getDesktop().browse(homepage);
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
    }
}

如果将应用程序打包为
WAR
文件,配置应用程序服务器,如
Tomcat
,然后通过IDE重新启动配置的应用程序服务器,IDE可以自动打开浏览器选项卡

如果要将应用程序打包为
JAR
文件,IDE将无法打开web浏览器,因此必须打开web浏览器并键入所需链接(
localhost:8080
)。但在发展阶段,采取这些无聊的步骤可能会非常乏味

在spring boot应用程序准备就绪后,可以使用Java编程语言打开浏览器。您可以使用第三方库,如
Selenium
,或使用以下代码段

@EventListener({ApplicationReadyEvent.class})
private void applicationReadyEvent()
{
    String url = "http://localhost:8080";

    // pointing to the download driver
    System.setProperty("webdriver.chrome.driver", "Downloaded-PATH/chromedriver");
    ChromeDriver chromeDriver = new ChromeDriver();
    chromeDriver.get(url);
}
打开浏览器的代码片段

@EventListener({ApplicationReadyEvent.class})
private void applicationReadyEvent()
{
    if (Desktop.isDesktopSupported())
    {
        Desktop desktop = Desktop.getDesktop();
        try
        {
            desktop.browse(new URI(url));
        } catch (IOException | URISyntaxException e)
        {
            e.printStackTrace();
        }
    } else
    {
        Runtime runtime = Runtime.getRuntime();
        String[] command;

        String operatingSystemName = System.getProperty("os.name").toLowerCase();
        if (operatingSystemName.indexOf("nix") >= 0 || operatingSystemName.indexOf("nux") >= 0)
        {
            String[] browsers = {"opera", "google-chrome", "epiphany", "firefox", "mozilla", "konqueror", "netscape", "links", "lynx"};
            StringBuffer stringBuffer = new StringBuffer();

            for (int i = 0; i < browsers.length; i++)
            {
                if (i == 0) stringBuffer.append(String.format("%s \"%s\"", browsers[i], url));
                else stringBuffer.append(String.format(" || %s \"%s\"", browsers[i], url));
            }
            command = new String[]{"sh", "-c", stringBuffer.toString()};
        } else if (operatingSystemName.indexOf("win") >= 0)
        {
            command = new String[]{"rundll32 url.dll,FileProtocolHandler " + url};

        } else if (operatingSystemName.indexOf("mac") >= 0)
        {
            command = new String[]{"open " + url};
        } else
        {
            System.out.println("an unknown operating system!!");
            return;
        }

        try
        {
            if (command.length > 1) runtime.exec(command); // linux
            else runtime.exec(command[0]); // windows or mac
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }    
}
然后在主类中,添加以下代码段

@EventListener({ApplicationReadyEvent.class})
private void applicationReadyEvent()
{
    String url = "http://localhost:8080";

    // pointing to the download driver
    System.setProperty("webdriver.chrome.driver", "Downloaded-PATH/chromedriver");
    ChromeDriver chromeDriver = new ChromeDriver();
    chromeDriver.get(url);
}
注意:可以使用大多数流行浏览器,如
FirefoxDriver
OperaDriver
EdgeDriver
,但必须事先下载浏览器的驱动程序

Runtime rt = Runtime.getRuntime();
        try {
            rt.exec("cmd /c start chrome.exe https://localhost:8080");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
上面的代码对我有用。将chrome.exe更改为您选择的浏览器,并将Url更改为您选择的浏览器。 注意:您必须包含方案-http或https,并且您选择的浏览器必须已安装,否则您的应用程序将在不自动打开浏览器的情况下运行。
仅适用于windows尽管

也许我应该详细说明一下,我想得到一个类似beanpostconstruct的回调,让我知道web上下文是否已准备就绪,然后我可以像上面的代码一样打开浏览器或使用浏览器启动程序库。运行方法后,应用程序已准备就绪。。。所以你可以在那之后启动你的浏览器。你的意思是说run方法是一个阻塞调用?是的。run方法将为您提供一个完全配置的、因而已准备就绪的
ApplicationContext
Runtime rt = Runtime.getRuntime();
        try {
            rt.exec("cmd /c start chrome.exe https://localhost:8080");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }