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

