在項目中,時常會有異步調用的需求
web.xml配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< servlet > < 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添加配置:
1
2
3
|
<!-- 支持異步方法執行 --> < task:executor id = "myExecutor" pool-size = "10" /> < task:annotation-driven executor = "myExecutor" /> |
然后demo:
1
2
3
4
5
6
7
|
@Service @EnableAsync public class DevicesEditLogService { @Async public void recordEditLog(Map<String, Object> param) { } } |
類上添加@EnableAsync, 方法上添加@Async,
添加@Service, 其他類可以注入這個實例,并調用成員方法
注:有了解到,如果@Async修飾的方法和調用此方法的其他方法在同一個類中,不會生效
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/chenmz1995/p/12016007.html