Java web spring異步方法實現(xiàn)步驟解析
在項目中,時常會有異步調用的需求
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, 其他類可以注入這個實例,并調用成員方法
注:有了解到,如果@Async修飾的方法和調用此方法的其他方法在同一個類中,不會生效
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. 詳解Android studio 動態(tài)fragment的用法2. 編程語言PHP在Web開發(fā)領域的優(yōu)勢在哪?3. 什么是python的自省4. Spring Boot和Thymeleaf整合結合JPA實現(xiàn)分頁效果(實例代碼)5. 解決Android studio xml界面無法預覽問題6. 基于android studio的layout的xml文件的創(chuàng)建方式7. Android如何加載Base64編碼格式圖片8. Springboot Druid 自定義加密數(shù)據(jù)庫密碼的幾種方案9. Vue封裝一個TodoList的案例與瀏覽器本地緩存的應用實現(xiàn)10. 圖文詳解vue中proto文件的函數(shù)調用

網公網安備