我忘記我是參考哪篇博客了,如果我是參考您的,覺得不妥,請第一時間聯繫我,我進行下架
-
之前做SSM項目的時候,啓動
TOMCAT
的時候,成功是可以直接彈出項目首頁的,是 非常的方便,最近在做SpringBoot
的時候,啓動卻不彈出項目首頁,非常的不方便,所以百度了一番,實現了項目啓動,直接彈出首頁。 -
application.properties
文件,添加下方配置。
#啓動項目時是否自動彈出遊覽器
spring.auto.openurl=true
spring.web.loginurl=http://localhost:9098 //配置項目打開哪個頁面
spring.web.googleexcute=C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe //也可以更換其他遊覽器,我這裏選擇的谷歌
- 創建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());
}
}
}
}
- 完事,可以直接啓動項目查看了。