Skip to content

Commit 8c9b64c

Browse files
committed
added mode="proxy"/"aspectj" and proxy-target-class options to task namespace; switched to concise names for async aspects
1 parent 171f1ee commit 8c9b64c

File tree

10 files changed

+324
-218
lines changed

10 files changed

+324
-218
lines changed
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,18 @@ import org.springframework.core.task.support.TaskExecutorAdapter;
2828
/**
2929
* Abstract aspect that routes selected methods asynchronously.
3030
*
31-
* <p>This aspect, by default, uses {@link SimpleAsyncTaskExecutor} to route
32-
* method execution. However, you may inject it with any implementation of
33-
* {@link Executor} to override the default.
31+
* <p>This aspect needs to be injected with an implementation of
32+
* {@link Executor} to activate it for a specific thread pool.
33+
* Otherwise it will simply delegate all calls synchronously.
3434
*
3535
* @author Ramnivas Laddad
36+
* @author Juergen Hoeller
3637
* @since 3.0.5
3738
*/
38-
public abstract aspect AbstractAsynchronousExecutionAspect {
39+
public abstract aspect AbstractAsyncExecutionAspect {
3940

4041
private AsyncTaskExecutor asyncExecutor;
4142

42-
public AbstractAsynchronousExecutionAspect() {
43-
// Set default executor, which may be replaced by calling setExecutor.
44-
setExecutor(new SimpleAsyncTaskExecutor());
45-
}
46-
4743
public void setExecutor(Executor executor) {
4844
if (executor instanceof AsyncTaskExecutor) {
4945
this.asyncExecutor = (AsyncTaskExecutor) executor;
@@ -54,6 +50,9 @@ public abstract aspect AbstractAsynchronousExecutionAspect {
5450
}
5551

5652
Object around() : asyncMethod() {
53+
if (this.asyncExecutor == null) {
54+
return proceed();
55+
}
5756
Callable<Object> callable = new Callable<Object>() {
5857
public Object call() throws Exception {
5958
Object result = proceed();
@@ -62,7 +61,7 @@ public abstract aspect AbstractAsynchronousExecutionAspect {
6261
}
6362
return null;
6463
}};
65-
Future<?> result = asyncExecutor.submit(callable);
64+
Future<?> result = this.asyncExecutor.submit(callable);
6665
if (Future.class.isAssignableFrom(((MethodSignature) thisJoinPointStaticPart.getSignature()).getReturnType())) {
6766
return result;
6867
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import org.springframework.scheduling.annotation.Async;
3333
* @author Ramnivas Laddad
3434
* @since 3.0.5
3535
*/
36-
public aspect AnnotationDrivenAsynchronousExecutionAspect extends AbstractAsynchronousExecutionAspect {
36+
public aspect AnnotationAsyncExecutionAspect extends AbstractAsyncExecutionAspect {
3737

3838
private pointcut asyncMarkedMethod()
3939
: execution(@Async (void || Future+) *(..));
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,93 @@
1-
/*
2-
* Copyright 2002-2007 the original author or authors.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
17-
package org.springframework.transaction.aspectj;
18-
19-
import java.lang.reflect.Method;
20-
21-
import org.aspectj.lang.annotation.SuppressAjWarnings;
22-
import org.aspectj.lang.reflect.MethodSignature;
23-
import org.springframework.transaction.interceptor.TransactionAspectSupport;
24-
import org.springframework.transaction.interceptor.TransactionAttributeSource;
25-
26-
/**
27-
* Abstract superaspect for AspectJ transaction aspects. Concrete
28-
* subaspects will implement the <code>transactionalMethodExecution()</code>
29-
* pointcut using a strategy such as Java 5 annotations.
30-
*
31-
* <p>Suitable for use inside or outside the Spring IoC container.
32-
* Set the "transactionManager" property appropriately, allowing
33-
* use of any transaction implementation supported by Spring.
34-
*
35-
* <p><b>NB:</b> If a method implements an interface that is itself
36-
* transactionally annotated, the relevant Spring transaction attribute
37-
* will <i>not</i> be resolved. This behavior will vary from that of Spring AOP
38-
* if proxying an interface (but not when proxying a class). We recommend that
39-
* transaction annotations should be added to classes, rather than business
40-
* interfaces, as they are an implementation detail rather than a contract
41-
* specification validation.
42-
*
43-
* @author Rod Johnson
44-
* @author Ramnivas Laddad
45-
* @since 2.0
46-
*/
47-
public abstract aspect AbstractTransactionAspect extends TransactionAspectSupport {
48-
49-
/**
50-
* Construct object using the given transaction metadata retrieval strategy.
51-
* @param tas TransactionAttributeSource implementation, retrieving Spring
52-
* transaction metadata for each joinpoint. Write the subclass to pass in null
53-
* if it's intended to be configured by Setter Injection.
54-
*/
55-
protected AbstractTransactionAspect(TransactionAttributeSource tas) {
56-
setTransactionAttributeSource(tas);
57-
}
58-
59-
@SuppressAjWarnings("adviceDidNotMatch")
60-
before(Object txObject) : transactionalMethodExecution(txObject) {
61-
MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature();
62-
Method method = methodSignature.getMethod();
63-
TransactionInfo txInfo = createTransactionIfNecessary(method, txObject.getClass());
64-
}
65-
66-
@SuppressAjWarnings("adviceDidNotMatch")
67-
after(Object txObject) throwing(Throwable t) : transactionalMethodExecution(txObject) {
68-
try {
69-
completeTransactionAfterThrowing(TransactionAspectSupport.currentTransactionInfo(), t);
70-
}
71-
catch (Throwable t2) {
72-
logger.error("Failed to close transaction after throwing in a transactional method", t2);
73-
}
74-
}
75-
76-
@SuppressAjWarnings("adviceDidNotMatch")
77-
after(Object txObject) returning() : transactionalMethodExecution(txObject) {
78-
commitTransactionAfterReturning(TransactionAspectSupport.currentTransactionInfo());
79-
}
80-
81-
@SuppressAjWarnings("adviceDidNotMatch")
82-
after(Object txObject) : transactionalMethodExecution(txObject) {
83-
cleanupTransactionInfo(TransactionAspectSupport.currentTransactionInfo());
84-
}
85-
86-
/**
87-
* Concrete subaspects must implement this pointcut, to identify
88-
* transactional methods. For each selected joinpoint, TransactionMetadata
89-
* will be retrieved using Spring's TransactionAttributeSource interface.
90-
*/
91-
protected abstract pointcut transactionalMethodExecution(Object txObject);
92-
93-
}
1+
/*
2+
* Copyright 2002-2010 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.transaction.aspectj;
18+
19+
import java.lang.reflect.Method;
20+
21+
import org.aspectj.lang.annotation.SuppressAjWarnings;
22+
import org.aspectj.lang.reflect.MethodSignature;
23+
import org.springframework.transaction.interceptor.TransactionAspectSupport;
24+
import org.springframework.transaction.interceptor.TransactionAttributeSource;
25+
26+
/**
27+
* Abstract superaspect for AspectJ transaction aspects. Concrete
28+
* subaspects will implement the <code>transactionalMethodExecution()</code>
29+
* pointcut using a strategy such as Java 5 annotations.
30+
*
31+
* <p>Suitable for use inside or outside the Spring IoC container.
32+
* Set the "transactionManager" property appropriately, allowing
33+
* use of any transaction implementation supported by Spring.
34+
*
35+
* <p><b>NB:</b> If a method implements an interface that is itself
36+
* transactionally annotated, the relevant Spring transaction attribute
37+
* will <i>not</i> be resolved. This behavior will vary from that of Spring AOP
38+
* if proxying an interface (but not when proxying a class). We recommend that
39+
* transaction annotations should be added to classes, rather than business
40+
* interfaces, as they are an implementation detail rather than a contract
41+
* specification validation.
42+
*
43+
* @author Rod Johnson
44+
* @author Ramnivas Laddad
45+
* @since 2.0
46+
*/
47+
public abstract aspect AbstractTransactionAspect extends TransactionAspectSupport {
48+
49+
/**
50+
* Construct object using the given transaction metadata retrieval strategy.
51+
* @param tas TransactionAttributeSource implementation, retrieving Spring
52+
* transaction metadata for each joinpoint. Write the subclass to pass in null
53+
* if it's intended to be configured by Setter Injection.
54+
*/
55+
protected AbstractTransactionAspect(TransactionAttributeSource tas) {
56+
setTransactionAttributeSource(tas);
57+
}
58+
59+
@SuppressAjWarnings("adviceDidNotMatch")
60+
before(Object txObject) : transactionalMethodExecution(txObject) {
61+
MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature();
62+
Method method = methodSignature.getMethod();
63+
TransactionInfo txInfo = createTransactionIfNecessary(method, txObject.getClass());
64+
}
65+
66+
@SuppressAjWarnings("adviceDidNotMatch")
67+
after(Object txObject) throwing(Throwable t) : transactionalMethodExecution(txObject) {
68+
try {
69+
completeTransactionAfterThrowing(TransactionAspectSupport.currentTransactionInfo(), t);
70+
}
71+
catch (Throwable t2) {
72+
logger.error("Failed to close transaction after throwing in a transactional method", t2);
73+
}
74+
}
75+
76+
@SuppressAjWarnings("adviceDidNotMatch")
77+
after(Object txObject) returning() : transactionalMethodExecution(txObject) {
78+
commitTransactionAfterReturning(TransactionAspectSupport.currentTransactionInfo());
79+
}
80+
81+
@SuppressAjWarnings("adviceDidNotMatch")
82+
after(Object txObject) : transactionalMethodExecution(txObject) {
83+
cleanupTransactionInfo(TransactionAspectSupport.currentTransactionInfo());
84+
}
85+
86+
/**
87+
* Concrete subaspects must implement this pointcut, to identify
88+
* transactional methods. For each selected joinpoint, TransactionMetadata
89+
* will be retrieved using Spring's TransactionAttributeSource interface.
90+
*/
91+
protected abstract pointcut transactionalMethodExecution(Object txObject);
92+
93+
}
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,75 @@
1-
/*
2-
* Copyright 2002-2008 the original author or authors.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
17-
package org.springframework.transaction.aspectj;
18-
19-
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
20-
import org.springframework.transaction.annotation.Transactional;
21-
22-
/**
23-
* Concrete AspectJ transaction aspect using Spring Transactional annotation
24-
* for JDK 1.5+.
25-
*
26-
* <p>When using this aspect, you <i>must</i> annotate the implementation class
27-
* (and/or methods within that class), <i>not</i> the interface (if any) that
28-
* the class implements. AspectJ follows Java's rule that annotations on
29-
* interfaces are <i>not</i> inherited.
30-
*
31-
* <p>A @Transactional annotation on a class specifies the default transaction
32-
* semantics for the execution of any <b>public</b> operation in the class.
33-
*
34-
* <p>A @Transactional annotation on a method within the class overrides the
35-
* default transaction semantics given by the class annotation (if present).
36-
* Any method may be annotated (regardless of visibility).
37-
* Annotating non-public methods directly is the only way
38-
* to get transaction demarcation for the execution of such operations.
39-
*
40-
* @author Rod Johnson
41-
* @author Ramnivas Laddad
42-
* @author Adrian Colyer
43-
* @since 2.0
44-
* @see org.springframework.transaction.annotation.Transactional
45-
*/
46-
public aspect AnnotationTransactionAspect extends AbstractTransactionAspect {
47-
48-
public AnnotationTransactionAspect() {
49-
super(new AnnotationTransactionAttributeSource(false));
50-
}
51-
52-
/**
53-
* Matches the execution of any public method in a type with the
54-
* Transactional annotation, or any subtype of a type with the
55-
* Transactional annotation.
56-
*/
57-
private pointcut executionOfAnyPublicMethodInAtTransactionalType() :
58-
execution(public * ((@Transactional *)+).*(..)) && @this(Transactional);
59-
60-
/**
61-
* Matches the execution of any method with the
62-
* Transactional annotation.
63-
*/
64-
private pointcut executionOfTransactionalMethod() :
65-
execution(* *(..)) && @annotation(Transactional);
66-
67-
/**
68-
* Definition of pointcut from super aspect - matched join points
69-
* will have Spring transaction management applied.
70-
*/
71-
protected pointcut transactionalMethodExecution(Object txObject) :
72-
(executionOfAnyPublicMethodInAtTransactionalType()
73-
|| executionOfTransactionalMethod() )
74-
&& this(txObject);
75-
76-
}
1+
/*
2+
* Copyright 2002-2010 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.transaction.aspectj;
18+
19+
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
20+
import org.springframework.transaction.annotation.Transactional;
21+
22+
/**
23+
* Concrete AspectJ transaction aspect using Spring's @Transactional annotation.
24+
*
25+
* <p>When using this aspect, you <i>must</i> annotate the implementation class
26+
* (and/or methods within that class), <i>not</i> the interface (if any) that
27+
* the class implements. AspectJ follows Java's rule that annotations on
28+
* interfaces are <i>not</i> inherited.
29+
*
30+
* <p>A @Transactional annotation on a class specifies the default transaction
31+
* semantics for the execution of any <b>public</b> operation in the class.
32+
*
33+
* <p>A @Transactional annotation on a method within the class overrides the
34+
* default transaction semantics given by the class annotation (if present).
35+
* Any method may be annotated (regardless of visibility).
36+
* Annotating non-public methods directly is the only way
37+
* to get transaction demarcation for the execution of such operations.
38+
*
39+
* @author Rod Johnson
40+
* @author Ramnivas Laddad
41+
* @author Adrian Colyer
42+
* @since 2.0
43+
* @see org.springframework.transaction.annotation.Transactional
44+
*/
45+
public aspect AnnotationTransactionAspect extends AbstractTransactionAspect {
46+
47+
public AnnotationTransactionAspect() {
48+
super(new AnnotationTransactionAttributeSource(false));
49+
}
50+
51+
/**
52+
* Matches the execution of any public method in a type with the
53+
* Transactional annotation, or any subtype of a type with the
54+
* Transactional annotation.
55+
*/
56+
private pointcut executionOfAnyPublicMethodInAtTransactionalType() :
57+
execution(public * ((@Transactional *)+).*(..)) && @this(Transactional);
58+
59+
/**
60+
* Matches the execution of any method with the
61+
* Transactional annotation.
62+
*/
63+
private pointcut executionOfTransactionalMethod() :
64+
execution(* *(..)) && @annotation(Transactional);
65+
66+
/**
67+
* Definition of pointcut from super aspect - matched join points
68+
* will have Spring transaction management applied.
69+
*/
70+
protected pointcut transactionalMethodExecution(Object txObject) :
71+
(executionOfAnyPublicMethodInAtTransactionalType()
72+
|| executionOfTransactionalMethod() )
73+
&& this(txObject);
74+
75+
}

0 commit comments

Comments
 (0)