springboot 项目启动自动打开浏览器访问网站设置
在appcation.yml中添加以下代码:
openProject:
isOpen: true
cmd: cmd /c start
web:
openUrl: http://localhost:8080
添加配置RunConfig类:
package com.example.demo.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RunConfig implements CommandLineRunner {
@Value("${openProject.isOpen}")
private boolean isOpen;
@Value("${openProject.web.openUrl}")
private String openUrl;
@Value("${openProject.cmd}")
private String cmd;
@Override
public void run(String... args){
if(isOpen){
String runCmd = cmd + " " + openUrl ;
System.out.println("运行的命令: " + runCmd);
Runtime run = Runtime.getRuntime();
try {
run.exec(runCmd);
System.out.println("启动浏览器打开项目成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("启动项目自动打开浏览器失败");
}
}
}
}
成功自动跳转到登录页面:
太棒了!
你记得
收藏
一下!容易找!