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ì)生效
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. PHP字符串前后字符或空格刪除方法介紹2. css進(jìn)階學(xué)習(xí) 選擇符3. XML入門的常見問題(一)4. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法5. Laravel操作session和cookie的教程詳解6. jsp實(shí)現(xiàn)登錄界面7. Echarts通過dataset數(shù)據(jù)集實(shí)現(xiàn)創(chuàng)建單軸散點(diǎn)圖8. html小技巧之td,div標(biāo)簽里內(nèi)容不換行9. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式10. 解析原生JS getComputedStyle
