Java web spring異步方法實(shí)現(xiàn)步驟解析
在項(xiàng)目中,時(shí)常會(huì)有異步調(diào)用的需求
web.xml配置
<servlet> <description>spring mvc servlet</description> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring mvc 配置文件</description> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
添加:<async-supported>true</async-supported>
spring xml添加配置:
<!-- 支持異步方法執(zhí)行 --> <task:executor id="myExecutor" pool-size="10" /> <task:annotation-driven executor="myExecutor"/>
然后demo:
@Service
@EnableAsync
public class DevicesEditLogService {
@Async
public void recordEditLog(Map<String, Object> param) {
}
}
類上添加@EnableAsync, 方法上添加@Async,
添加@Service, 其他類可以注入這個(gè)實(shí)例,并調(diào)用成員方法
注:有了解到,如果@Async修飾的方法和調(diào)用此方法的其他方法在同一個(gè)類中,不會(huì)生效
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot+webmagic實(shí)現(xiàn)java爬蟲jdbc及mysql的方法
- Java中Spring WebSocket詳解
- webuploader在springMVC+jquery+Java開發(fā)環(huán)境下的大文件分片上傳的實(shí)例代碼
- java web SpringMVC后端傳json數(shù)據(jù)到前端頁面實(shí)例代碼
- 詳解java WebSocket的實(shí)現(xiàn)以及Spring WebSocket
- java WebSocket的實(shí)現(xiàn)以及Spring WebSocket示例代碼
- C程序函數(shù)調(diào)用&系統(tǒng)調(diào)用
- Java Web項(xiàng)目中Spring框架處理JSON格式數(shù)據(jù)的方法
相關(guān)文章
Java MongoDB數(shù)據(jù)庫連接方法梳理
MongoDB作為一種介于關(guān)系型數(shù)據(jù)庫和非關(guān)系型數(shù)據(jù)庫之間的產(chǎn)品,它可以提供可擴(kuò)展的高性能的數(shù)據(jù)存儲(chǔ)解決方案,近些年來受到了開發(fā)者的喜愛2022-08-08
解析Java的JNI編程中的對(duì)象引用與內(nèi)存泄漏問題
這篇文章主要介紹了Java的JNI編程中的對(duì)象引用與內(nèi)存泄漏問題,重點(diǎn)講述了局部和全局引用時(shí)一些值得注意的地方,需要的朋友可以參考下2015-11-11
Springboot整合JwtHelper實(shí)現(xiàn)非對(duì)稱加密
本文主要介紹了Springboot整合JwtHelper實(shí)現(xiàn)非對(duì)稱加密,主要介紹兩種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
解讀Eureka的TimedSupervisorTask類(自動(dòng)調(diào)節(jié)間隔的周期性任務(wù))
在Eureka客戶端中,盡管ScheduledExecutorService的schedule方法創(chuàng)建的是一次性任務(wù),但通過在任務(wù)執(zhí)行完畢后再次調(diào)用schedule方法實(shí)現(xiàn)了周期性執(zhí)行,這種設(shè)計(jì)既考慮到了任務(wù)超時(shí)導(dǎo)致的間隔時(shí)間調(diào)整,又通過CAS實(shí)現(xiàn)了多線程同步,展現(xiàn)了簡潔而巧妙的設(shè)計(jì)思想2024-11-11

