Java web spring異步方法實現步驟解析
在項目中,時常會有異步調用的需求
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添加配置:
<!-- 支持異步方法執行 --> <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修飾的方法和調用此方法的其他方法在同一個類中,不會生效
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: