spring的@Async异步使用

pring的@Async功能,用的时候一定要注意:

1、异步方法和调用类不要在同一个类中。

2、xml里需要加入这一行 <task:annotation-driven/>

下面的可以直接粘贴使用

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd">

以下是demo

@Autowired
private AsyncDemo asyncDemo; @Action("/a")
public String a() throws Exception {
asyncDemo.asyncInvokeWithParameter(paramString("t"));
System.out.println(1111);
return null;
}
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; @Service
public class AsyncDemo { @Async
public void asyncInvokeWithParameter(String s) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("asyncInvokeWithParameter, parementer={}"+ s);
} }
上一篇:iOS coreData问题


下一篇:Spring Boot Async异步执行