springboot項目啟動后,自動打開默認(rèn)瀏覽器的方式
手動給spring加入監(jiān)聽任務(wù)
1、先寫1個線程類,在springboot啟動加載完,自動執(zhí)行的操作放在里面
@Component
public class StepExecutor implements Runnable {
@Override
public void run() {
startStreamTask();
}
/**
* 項目啟動后打開1個頁面
*/
public void startStreamTask() {
try {
Runtime.getRuntime().exec("cmd /c start http://localhost:8080/index");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
2、寫1個監(jiān)聽器,監(jiān)聽項目加載完后執(zhí)行指定操作
public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext ac = event.getApplicationContext();
StepExecutor StepExecutor = ac.getBean(StepExecutor .class);
Thread thread = new Thread(StepExecutor);
thread.start();
}
}
3、給springboot的啟動類添加監(jiān)聽任務(wù)
@SpringBootApplication
public class SpeechApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(SpeechApplication .class);
springApplication.addListeners(new ApplicationStartup());
springApplication.run(args);
}
}
spring容器加載完自動監(jiān)聽
1、實現(xiàn)springboot默認(rèn)的監(jiān)聽接口,該方法在spring容器加載完自動監(jiān)聽
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
* 配置自動啟動瀏覽器
*/
@Component
public class MyCommandRunner implements CommandLineRunner {
@Value("${server.port}")
private String port;
@Override
public void run(String... args) {
try {
Runtime.getRuntime().exec("cmd /c start http://localhost:" + port + "/index");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
2、正常啟動
@EnableWebMvc
@SpringBootApplication
public class PhotoShowApplication {
public static void main(String[] args) {
SpringApplication.run(PhotoShowApplication.class, args);
}
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中流式并行操作parallelStream的原理和使用方法
本文詳細(xì)介紹了Java中的并行流(parallelStream)的原理、正確使用方法以及在實際業(yè)務(wù)中的應(yīng)用案例,并指出在使用并行流時需要注意的線程安全問題和性能問題,并提供了最佳實踐建議,感興趣的朋友跟隨小編一起看看吧2025-11-11
Spring Boot 中的默認(rèn)異常處理機制及執(zhí)行流程
SpringBoot內(nèi)置BasicErrorController,自動處理異常并生成HTML/JSON響應(yīng),支持自定義錯誤路徑、配置及擴展,如ErrorController和@ControllerAdvice,實現(xiàn)靈活的錯誤處理機制,對Spring Boot 默認(rèn)異常處理機制相關(guān)知識感興趣的朋友一起看看吧2025-07-07
java線程池合理設(shè)置最大線程數(shù)和核心線程數(shù)方式
這篇文章主要介紹了java線程池合理設(shè)置最大線程數(shù)和核心線程數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
mybatis根據(jù)表逆向自動化生成代碼的實現(xiàn)
若采用mybatis框架,數(shù)據(jù)庫新建表,手動編寫的話,需要編寫大量的實體類、mapper文件、mapper.xml文件,都是一些重復(fù)且有規(guī)律的工作。我們可以引用插件,然后做配置,自動生成這些文件,本文就來詳細(xì)的介紹一下2021-08-08
在SpringBoot中集成H2數(shù)據(jù)庫的完整指南
Spring Boot是一個簡化企業(yè)級Java應(yīng)用程序開發(fā)的強大框架,H2數(shù)據(jù)庫是一個輕量級的、開源的SQL數(shù)據(jù)庫,非常適合用于開發(fā)和測試,本文將指導(dǎo)您如何在Spring Boot應(yīng)用程序中集成H2數(shù)據(jù)庫,并探索一些高級配置選項,需要的朋友可以參考下2024-10-10
rabbitmq學(xué)習(xí)系列教程之消息應(yīng)答(autoAck)、隊列持久化(durable)及消息持久化
這篇文章主要介紹了rabbitmq學(xué)習(xí)系列教程之消息應(yīng)答(autoAck)、隊列持久化(durable)及消息持久化,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
java對象和json的來回轉(zhuǎn)換知識點總結(jié)
在本篇文章里小編給大家分享了一篇關(guān)于java對象和json的來回轉(zhuǎn)換知識點總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-01-01
springBoot集成shiro實現(xiàn)權(quán)限刷新
在SpringBoot項目中集成Shiro進行權(quán)限管理,包括基礎(chǔ)配置引入依賴、創(chuàng)建Shiro配置類以及用戶認(rèn)證與授權(quán)實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-11-11
Spring Security中的 @PreAuthorize 注解使用方法和示例代碼
@PreAuthorize是SpringSecurity中用于方法級權(quán)限控制的重要注解,本文詳細(xì)介紹了@PreAuthorize的使用方法,通過合理使用@PreAuthorize,可以實現(xiàn)細(xì)粒度的權(quán)限控制,確保應(yīng)用程序的安全性,感興趣的朋友跟隨小編一起看看吧2026-01-01

