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 pool-size='10' /> <task:annotation-driven executor='myExecutor'/>
然后demo:
@Service@EnableAsyncpublic class DevicesEditLogService { @Async public void recordEditLog(Map<String, Object> param) { }}
類上添加@EnableAsync, 方法上添加@Async,
添加@Service, 其他類可以注入這個(gè)實(shí)例,并調(diào)用成員方法
注:有了解到,如果@Async修飾的方法和調(diào)用此方法的其他方法在同一個(gè)類中,不會(huì)生效
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 使用Python webdriver圖書館搶座自動(dòng)預(yù)約的正確方法2. Linux刪除系統(tǒng)自帶版本Python過程詳解3. ASP基礎(chǔ)知識(shí)VBScript基本元素講解4. Python 合并拼接字符串的方法5. Python 利用Entrez庫(kù)篩選下載PubMed文獻(xiàn)摘要的示例6. Python3 json模塊之編碼解碼方法講解7. Python 制作查詢商品歷史價(jià)格的小工具8. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條9. Python sublime安裝及配置過程詳解10. python 使用事件對(duì)象asyncio.Event來同步協(xié)程的操作

網(wǎng)公網(wǎng)安備