Spingboot项目启动,游览器自动打开项目。

我忘记我是参考哪篇博客了,如果我是参考您的,觉得不妥,请第一时间联系我,我进行下架

  1. 之前做SSM项目的时候,启动TOMCAT的时候,成功是可以直接弹出项目首页的,是 非常的方便,最近在做SpringBoot的时候,启动却不弹出项目首页,非常的不方便,所以百度了一番,实现了项目启动,直接弹出首页。

  2. application.properties文件,添加下方配置。

   #启动项目时是否自动弹出游览器
   spring.auto.openurl=true
   spring.web.loginurl=http://localhost:9098  //配置项目打开哪个页面
   spring.web.googleexcute=C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe //也可以更换其他游览器,我这里选择的谷歌
  1. 创建JAVA配置类。
   import org.slf4j.Logger;
   import org.slf4j.LoggerFactory;
   import org.springframework.beans.factory.annotation.Value;
   import org.springframework.boot.CommandLineRunner;
   import org.springframework.stereotype.Component;
   
   @Component
   public class MyCommandRunner implements CommandLineRunner {
       private static Logger logger = LoggerFactory.getLogger(MyCommandRunner.class);
       @Value("${spring.web.loginurl}")
       private String loginUrl;
   
       @Value("${spring.web.googleexcute}")
       private String googleExcutePath;
   
       @Value("${spring.auto.openurl}")
       private boolean isOpen;
   
       @Override
       public void run(String... args) throws Exception {
           if(isOpen){
               String cmd = googleExcutePath +" "+ loginUrl;
               Runtime run = Runtime.getRuntime();
               try{
                   run.exec(cmd);
                   logger.debug("启动浏览器打开项目成功");
               }catch (Exception e){
                   e.printStackTrace();
                   logger.error(e.getMessage());
               }
           }
       }
   }
  1. 完事,可以直接启动项目查看了。
发布了6 篇原创文章 · 获赞 3 · 访问量 2142
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章