最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

SpringBoot项目启动后自动打开浏览器

运维笔记admin13浏览0评论

 

编写一个类,注册为Spring的Bean,然后实现CommandLineRunner接口,重写run()方法即可

 

package com.example.demo.config;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class OpenBrowser implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("应用已经准备就绪 ... 启动浏览器并自动加载指定的页面 ... ");
        try {
            Runtime.getRuntime().exec("cmd   /c   start   http://localhost:8080");//指定自己项目的路径
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

发布评论

评论列表(0)

  1. 暂无评论