SpringBoot中添加監(jiān)聽器及創(chuàng)建線程的代碼示例
Application主程序上添加注解
@ServletComponentScan(basePackages= {"com.qyj.listeners"})// 添加監(jiān)聽器所在的包名
監(jiān)聽程序
package com.qyj.listeners; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.StringRedisTemplate; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener public class AudioPlayListener implements ServletContextListener { @Autowired private StringRedisTemplate redisTemplate; @Value("${qyj.fee-type}") private String feeType; // 當(dāng)后臺被初始化,即發(fā)生了tomcat啟動了事件,固定用法 @Override public void contextInitialized(ServletContextEvent event) { // 你要做的事兒,寫在這里 System.out.println("AudioPlay is running(語音播報-循環(huán)監(jiān)聽中)..."); // 新建線程while循環(huán)處理語音播報 new Thread(new AudioPlayThread(redisTemplate, feeType)).start(); } // 當(dāng)后臺被銷毀,即發(fā)生了tomcat關(guān)閉了事件,固定用法 @Override public void contextDestroyed(ServletContextEvent event) { System.out.println("后臺被銷毀"); } }
新建線程
package com.qyj.listeners; import com.qyj.constant.RedisConstant; import com.qyj.utils.AudioPlayUtil; import org.springframework.data.redis.core.StringRedisTemplate; import java.util.concurrent.TimeUnit; public class AudioPlayThread implements Runnable{ // 因為線程是new出來的 所以此處不能再通過依賴注入獲取信息了 private String feeType; private StringRedisTemplate redisTemplate; public AudioPlayThread(StringRedisTemplate redisTemplate, String feeType) { this.redisTemplate = redisTemplate; this.feeType = feeType; } @Override public void run() { while(true){ try { System.out.println("這是繼承Thread類創(chuàng)建的線程"); } catch (Exception e) { e.printStackTrace(); } } } }
到此這篇關(guān)于SpringBoot中添加監(jiān)聽器及創(chuàng)建線程的代碼示例的文章就介紹到這了,更多相關(guān)SpringBoot 添加監(jiān)聽器及創(chuàng)建線程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Flyway進行Java數(shù)據(jù)庫版本控制的操作指南
今天我們將深入探討如何使用Flyway進行Java數(shù)據(jù)庫版本控制,Flyway是一個流行的數(shù)據(jù)庫遷移工具,用于管理和自動化數(shù)據(jù)庫模式的演變,文中通過代碼示例介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07Spring Cloud 服務(wù)網(wǎng)關(guān)Zuul的實現(xiàn)
這篇文章主要介紹了Spring Cloud 服務(wù)網(wǎng)關(guān)Zuul的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Java多線程之JUC(java.util.concurrent)的常見類(多線程編程常用類)
這篇文章主要給大家介紹了關(guān)于Java多線程之JUC(java.util.concurrent)的常見類(多線程編程常用類)的相關(guān)資料,Java中的JUC(java.util.concurrent)包提供了一些并發(fā)編程中常用的類,這些類可以幫助我們更方便地實現(xiàn)多線程編程,需要的朋友可以參考下2024-02-02SpringBoot中EasyExcel實現(xiàn)Excel文件的導(dǎo)入導(dǎo)出
這篇文章主要介紹了SpringBoot中EasyExcel實現(xiàn)Excel文件的導(dǎo)入導(dǎo)出,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10解讀動態(tài)數(shù)據(jù)源dynamic-datasource-spring-boot-starter使用問題
這篇文章主要介紹了解讀動態(tài)數(shù)據(jù)源dynamic-datasource-spring-boot-starter使用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03mybatis執(zhí)行錯誤但sql執(zhí)行正常問題
這篇文章主要介紹了mybatis執(zhí)行錯誤但sql執(zhí)行正常問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01