【问题标题】:Launch browser automatically after spring-boot webapp is readyspring-boot webapp 准备好后自动启动浏览器
【发布时间】:2014-12-09 11:50:38
【问题描述】:

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

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

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    以下代码对我有用:

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

    【讨论】:

      【解决方案2】:
      @SpringBootApplication
      @ComponentScan(basePackageClasses = com.io.controller.HelloController.class)
      public class HectorApplication {
      
          public static void main(String[] args) throws IOException {
             SpringApplication.run(HectorApplication.class, args);
             openHomePage();
          }
      
          private static void openHomePage() throws IOException {
             Runtime rt = Runtime.getRuntime();
             rt.exec("rundll32 url.dll,FileProtocolHandler " + "http://localhost:8080");
          }
      }
      

      【讨论】:

        【解决方案3】:

        你可以通过一些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();
                    }
                }
            }
        }
        

        【讨论】:

        • 也许我应该详细说明一下,我想得到一个像 beanpostconstruct 这样的回调,让我知道网络上下文是否准备好服务,然后我可以像上面的代码一样打开浏览器或使用浏览器启动器库.
        • 在运行方法之后,应用程序就准备好了......所以你可以在此之后启动你的浏览器。
        • 你的意思是说run方法是一个阻塞调用?
        • 是的。 run 方法会给你一个完全配置好的,因此准备好的ApplicationContext
        【解决方案4】:

        我最近一直在尝试自己解决这个问题,我知道这个问题被问到已经有一段时间了,但我的工作(并且非常基本/简单)的解决方案如下所示。对于任何想要使其正常工作的人来说,这是一个起点,可以根据您的应用程序的要求进行重构!

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

        【讨论】:

          【解决方案5】:

          如果你把应用打包成WAR文件,配置一个应用服务器,比如Tomcat,然后通过你的IDE重启配置的应用服务器,IDE可以自动打开一个浏览器标签。

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

          在 spring-boot 应用程序准备好后,可以使用 Java 编程语言打开浏览器。可以使用Selenium之类的第三方库或者使用如下代码sn-p。

          打开浏览器的代码sn-p

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

          使用 Selenium 打开浏览器

          要使用 selenium 库,请将以下依赖项添加到您的 pom.xml 文件中。

          <dependency>
              <groupId>org.seleniumhq.selenium</groupId>
              <artifactId>selenium-java</artifactId>
              <version>3.141.59</version>
          </dependency>
          

          然后在你的主类中,添加以下代码sn-p。

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

          注意FirefoxDriverOperaDriverEdgeDriver等大多数流行浏览器都可以使用,但需要事先下载浏览器的驱动程序。

          【讨论】:

            【解决方案6】:
            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

            【讨论】:

              猜你喜欢
              • 2016-05-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-05-05
              • 2016-01-24
              • 1970-01-01
              • 1970-01-01
              • 2017-01-29
              相关资源
              最近更新 更多