我正在使用Spring 3.2.2.RELEASE版本。 @Async詮釋不按預期工作。Spring @Async不起作用
的applicationContext.xml
<?xml version="1.0" encoding="windows-1252"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- Enable AspectJ style of Spring AOP -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:component-scan base-package="com.pratik" />
<tx:annotation-driven />
import.java
public String async() {
final String asssetImportId = ObjectId.get().toHexString();
process.asyncTest();
logger.info("Ongoing");
return asssetImportId;
}
process.java
@Async
public void asyncTest() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("After Sleep");
}
的後睡眠日誌應打印最後卻是越來越打印持續
'的Thread.sleep應爲(1000)',是這兩種方法同樣的類 –
這裏有很多可能性,你沒有顯示開啓異步支持(你在Spring的一個過時的版本),你沒有顯示你從哪裏得到'process'變量。零時間 – chrylis
@ @ Async'的使用要求您將'@ EnableAsync'註釋添加到其中一個'@ Configuration'類。 –