Springboot里面有个ApplicationReadyEvent事件,该事件表示application应该初始化完成,可以准备接收请求。
想要在启动后打开浏览器,可以将执行代码放到这个事件当中。
/**
* @author LaZY(李志一)
* @create 2019-06-08 11:30
*/
@Configuration
public class IndexConfig{
@EventListener({ApplicationReadyEvent.class})
void applicationReadyEvent() {
System.out.println("应用已经准备就绪 ... 启动浏览器");
String url = "http://localhost:8090/index";
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
} catch (IOException e) {
e.printStackTrace();
}
}