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

        以前写Spring项目的时候,每次启动Tomcat可以自动打开并预览网页,但是后面使用SpringBoot项目(因为SpringBoot内置了Tomcat无法修改配置),每次启动项目然后再打开浏览器访问十分麻烦,通过编写一个代码类也可以实现类似的功能:

@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/boot01");//可以指定自己的路径
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

posted @ 2022-05-17 14:37  青栀i  阅读(750)  评论(0)    收藏  举报  来源