SpringBoot中添加監(jiān)聽(tīng)器及創(chuàng)建線程的代碼示例
Application主程序上添加注解
@ServletComponentScan(basePackages= {"com.qyj.listeners"})// 添加監(jiān)聽(tīng)器所在的包名監(jiān)聽(tīng)程序
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)后臺(tái)被初始化,即發(fā)生了tomcat啟動(dòng)了事件,固定用法
@Override
public void contextInitialized(ServletContextEvent event) {
// 你要做的事兒,寫在這里
System.out.println("AudioPlay is running(語(yǔ)音播報(bào)-循環(huán)監(jiān)聽(tīng)中)...");
// 新建線程while循環(huán)處理語(yǔ)音播報(bào)
new Thread(new AudioPlayThread(redisTemplate, feeType)).start();
}
// 當(dāng)后臺(tái)被銷毀,即發(fā)生了tomcat關(guān)閉了事件,固定用法
@Override
public void contextDestroyed(ServletContextEvent event) {
System.out.println("后臺(tái)被銷毀");
}
}新建線程
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{
// 因?yàn)榫€程是new出來(lái)的 所以此處不能再通過(guò)依賴注入獲取信息了
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)聽(tīng)器及創(chuàng)建線程的代碼示例的文章就介紹到這了,更多相關(guān)SpringBoot 添加監(jiān)聽(tīng)器及創(chuàng)建線程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot項(xiàng)目監(jiān)聽(tīng)器失效問(wèn)題解決
- SpringBoot如何監(jiān)控Redis中某個(gè)Key的變化(自定義監(jiān)聽(tīng)器)
- SpringBoot中使用監(jiān)聽(tīng)器的方法詳解
- SpringBoot監(jiān)聽(tīng)事件和處理事件程序示例詳解
- SpringBoot 過(guò)濾器, 攔截器, 監(jiān)聽(tīng)器的具體使用
- SpringBoot監(jiān)聽(tīng)器的實(shí)現(xiàn)示例
- SpringBoot自定義監(jiān)聽(tīng)器的項(xiàng)目實(shí)踐
相關(guān)文章
使用Flyway進(jìn)行Java數(shù)據(jù)庫(kù)版本控制的操作指南
今天我們將深入探討如何使用Flyway進(jìn)行Java數(shù)據(jù)庫(kù)版本控制,Flyway是一個(gè)流行的數(shù)據(jù)庫(kù)遷移工具,用于管理和自動(dòng)化數(shù)據(jù)庫(kù)模式的演變,文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07
Spring Cloud 服務(wù)網(wǎng)關(guān)Zuul的實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud 服務(wù)網(wǎng)關(guān)Zuul的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Java多線程之JUC(java.util.concurrent)的常見(jiàn)類(多線程編程常用類)
這篇文章主要給大家介紹了關(guān)于Java多線程之JUC(java.util.concurrent)的常見(jiàn)類(多線程編程常用類)的相關(guān)資料,Java中的JUC(java.util.concurrent)包提供了一些并發(fā)編程中常用的類,這些類可以幫助我們更方便地實(shí)現(xiàn)多線程編程,需要的朋友可以參考下2024-02-02
SpringBoot中EasyExcel實(shí)現(xiàn)Excel文件的導(dǎo)入導(dǎo)出
這篇文章主要介紹了SpringBoot中EasyExcel實(shí)現(xiàn)Excel文件的導(dǎo)入導(dǎo)出,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
解讀動(dòng)態(tài)數(shù)據(jù)源dynamic-datasource-spring-boot-starter使用問(wèn)題
這篇文章主要介紹了解讀動(dòng)態(tài)數(shù)據(jù)源dynamic-datasource-spring-boot-starter使用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
MyBatis-Plus執(zhí)行SQL分析打印過(guò)程
這篇文章主要介紹了MyBatis-Plus執(zhí)行SQL分析打印過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
mybatis執(zhí)行錯(cuò)誤但sql執(zhí)行正常問(wèn)題
這篇文章主要介紹了mybatis執(zhí)行錯(cuò)誤但sql執(zhí)行正常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

