Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3770 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
10 changed files with 324 additions and 218 deletions
@ -1,93 +1,93 @@ |
|||||||
/* |
/* |
||||||
* Copyright 2002-2007 the original author or authors. |
* Copyright 2002-2010 the original author or authors. |
||||||
* |
* |
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
* you may not use this file except in compliance with the License. |
* you may not use this file except in compliance with the License. |
||||||
* You may obtain a copy of the License at |
* You may obtain a copy of the License at |
||||||
* |
* |
||||||
* http://www.apache.org/licenses/LICENSE-2.0 |
* http://www.apache.org/licenses/LICENSE-2.0 |
||||||
* |
* |
||||||
* Unless required by applicable law or agreed to in writing, software |
* Unless required by applicable law or agreed to in writing, software |
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
* See the License for the specific language governing permissions and |
* See the License for the specific language governing permissions and |
||||||
* limitations under the License. |
* limitations under the License. |
||||||
*/ |
*/ |
||||||
|
|
||||||
package org.springframework.transaction.aspectj; |
package org.springframework.transaction.aspectj; |
||||||
|
|
||||||
import java.lang.reflect.Method; |
import java.lang.reflect.Method; |
||||||
|
|
||||||
import org.aspectj.lang.annotation.SuppressAjWarnings; |
import org.aspectj.lang.annotation.SuppressAjWarnings; |
||||||
import org.aspectj.lang.reflect.MethodSignature; |
import org.aspectj.lang.reflect.MethodSignature; |
||||||
import org.springframework.transaction.interceptor.TransactionAspectSupport; |
import org.springframework.transaction.interceptor.TransactionAspectSupport; |
||||||
import org.springframework.transaction.interceptor.TransactionAttributeSource; |
import org.springframework.transaction.interceptor.TransactionAttributeSource; |
||||||
|
|
||||||
/** |
/** |
||||||
* Abstract superaspect for AspectJ transaction aspects. Concrete |
* Abstract superaspect for AspectJ transaction aspects. Concrete |
||||||
* subaspects will implement the <code>transactionalMethodExecution()</code> |
* subaspects will implement the <code>transactionalMethodExecution()</code> |
||||||
* pointcut using a strategy such as Java 5 annotations. |
* pointcut using a strategy such as Java 5 annotations. |
||||||
* |
* |
||||||
* <p>Suitable for use inside or outside the Spring IoC container. |
* <p>Suitable for use inside or outside the Spring IoC container. |
||||||
* Set the "transactionManager" property appropriately, allowing |
* Set the "transactionManager" property appropriately, allowing |
||||||
* use of any transaction implementation supported by Spring. |
* use of any transaction implementation supported by Spring. |
||||||
* |
* |
||||||
* <p><b>NB:</b> If a method implements an interface that is itself |
* <p><b>NB:</b> If a method implements an interface that is itself |
||||||
* transactionally annotated, the relevant Spring transaction attribute |
* transactionally annotated, the relevant Spring transaction attribute |
||||||
* will <i>not</i> be resolved. This behavior will vary from that of Spring AOP |
* will <i>not</i> be resolved. This behavior will vary from that of Spring AOP |
||||||
* if proxying an interface (but not when proxying a class). We recommend that |
* if proxying an interface (but not when proxying a class). We recommend that |
||||||
* transaction annotations should be added to classes, rather than business |
* transaction annotations should be added to classes, rather than business |
||||||
* interfaces, as they are an implementation detail rather than a contract |
* interfaces, as they are an implementation detail rather than a contract |
||||||
* specification validation. |
* specification validation. |
||||||
* |
* |
||||||
* @author Rod Johnson |
* @author Rod Johnson |
||||||
* @author Ramnivas Laddad |
* @author Ramnivas Laddad |
||||||
* @since 2.0 |
* @since 2.0 |
||||||
*/ |
*/ |
||||||
public abstract aspect AbstractTransactionAspect extends TransactionAspectSupport { |
public abstract aspect AbstractTransactionAspect extends TransactionAspectSupport { |
||||||
|
|
||||||
/** |
/** |
||||||
* Construct object using the given transaction metadata retrieval strategy. |
* Construct object using the given transaction metadata retrieval strategy. |
||||||
* @param tas TransactionAttributeSource implementation, retrieving Spring |
* @param tas TransactionAttributeSource implementation, retrieving Spring |
||||||
* transaction metadata for each joinpoint. Write the subclass to pass in null |
* transaction metadata for each joinpoint. Write the subclass to pass in null |
||||||
* if it's intended to be configured by Setter Injection. |
* if it's intended to be configured by Setter Injection. |
||||||
*/ |
*/ |
||||||
protected AbstractTransactionAspect(TransactionAttributeSource tas) { |
protected AbstractTransactionAspect(TransactionAttributeSource tas) { |
||||||
setTransactionAttributeSource(tas); |
setTransactionAttributeSource(tas); |
||||||
} |
} |
||||||
|
|
||||||
@SuppressAjWarnings("adviceDidNotMatch") |
@SuppressAjWarnings("adviceDidNotMatch") |
||||||
before(Object txObject) : transactionalMethodExecution(txObject) { |
before(Object txObject) : transactionalMethodExecution(txObject) { |
||||||
MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature(); |
MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature(); |
||||||
Method method = methodSignature.getMethod(); |
Method method = methodSignature.getMethod(); |
||||||
TransactionInfo txInfo = createTransactionIfNecessary(method, txObject.getClass()); |
TransactionInfo txInfo = createTransactionIfNecessary(method, txObject.getClass()); |
||||||
} |
} |
||||||
|
|
||||||
@SuppressAjWarnings("adviceDidNotMatch") |
@SuppressAjWarnings("adviceDidNotMatch") |
||||||
after(Object txObject) throwing(Throwable t) : transactionalMethodExecution(txObject) { |
after(Object txObject) throwing(Throwable t) : transactionalMethodExecution(txObject) { |
||||||
try { |
try { |
||||||
completeTransactionAfterThrowing(TransactionAspectSupport.currentTransactionInfo(), t); |
completeTransactionAfterThrowing(TransactionAspectSupport.currentTransactionInfo(), t); |
||||||
} |
} |
||||||
catch (Throwable t2) { |
catch (Throwable t2) { |
||||||
logger.error("Failed to close transaction after throwing in a transactional method", t2); |
logger.error("Failed to close transaction after throwing in a transactional method", t2); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
@SuppressAjWarnings("adviceDidNotMatch") |
@SuppressAjWarnings("adviceDidNotMatch") |
||||||
after(Object txObject) returning() : transactionalMethodExecution(txObject) { |
after(Object txObject) returning() : transactionalMethodExecution(txObject) { |
||||||
commitTransactionAfterReturning(TransactionAspectSupport.currentTransactionInfo()); |
commitTransactionAfterReturning(TransactionAspectSupport.currentTransactionInfo()); |
||||||
} |
} |
||||||
|
|
||||||
@SuppressAjWarnings("adviceDidNotMatch") |
@SuppressAjWarnings("adviceDidNotMatch") |
||||||
after(Object txObject) : transactionalMethodExecution(txObject) { |
after(Object txObject) : transactionalMethodExecution(txObject) { |
||||||
cleanupTransactionInfo(TransactionAspectSupport.currentTransactionInfo()); |
cleanupTransactionInfo(TransactionAspectSupport.currentTransactionInfo()); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Concrete subaspects must implement this pointcut, to identify |
* Concrete subaspects must implement this pointcut, to identify |
||||||
* transactional methods. For each selected joinpoint, TransactionMetadata |
* transactional methods. For each selected joinpoint, TransactionMetadata |
||||||
* will be retrieved using Spring's TransactionAttributeSource interface. |
* will be retrieved using Spring's TransactionAttributeSource interface. |
||||||
*/ |
*/ |
||||||
protected abstract pointcut transactionalMethodExecution(Object txObject); |
protected abstract pointcut transactionalMethodExecution(Object txObject); |
||||||
|
|
||||||
} |
} |
||||||
|
|||||||
@ -1,76 +1,75 @@ |
|||||||
/* |
/* |
||||||
* Copyright 2002-2008 the original author or authors. |
* Copyright 2002-2010 the original author or authors. |
||||||
* |
* |
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
* you may not use this file except in compliance with the License. |
* you may not use this file except in compliance with the License. |
||||||
* You may obtain a copy of the License at |
* You may obtain a copy of the License at |
||||||
* |
* |
||||||
* http://www.apache.org/licenses/LICENSE-2.0 |
* http://www.apache.org/licenses/LICENSE-2.0 |
||||||
* |
* |
||||||
* Unless required by applicable law or agreed to in writing, software |
* Unless required by applicable law or agreed to in writing, software |
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
* See the License for the specific language governing permissions and |
* See the License for the specific language governing permissions and |
||||||
* limitations under the License. |
* limitations under the License. |
||||||
*/ |
*/ |
||||||
|
|
||||||
package org.springframework.transaction.aspectj; |
package org.springframework.transaction.aspectj; |
||||||
|
|
||||||
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource; |
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource; |
||||||
import org.springframework.transaction.annotation.Transactional; |
import org.springframework.transaction.annotation.Transactional; |
||||||
|
|
||||||
/** |
/** |
||||||
* Concrete AspectJ transaction aspect using Spring Transactional annotation |
* Concrete AspectJ transaction aspect using Spring's @Transactional annotation. |
||||||
* for JDK 1.5+. |
* |
||||||
* |
* <p>When using this aspect, you <i>must</i> annotate the implementation class |
||||||
* <p>When using this aspect, you <i>must</i> annotate the implementation class |
* (and/or methods within that class), <i>not</i> the interface (if any) that |
||||||
* (and/or methods within that class), <i>not</i> the interface (if any) that |
* the class implements. AspectJ follows Java's rule that annotations on |
||||||
* the class implements. AspectJ follows Java's rule that annotations on |
* interfaces are <i>not</i> inherited. |
||||||
* interfaces are <i>not</i> inherited. |
* |
||||||
* |
* <p>A @Transactional annotation on a class specifies the default transaction |
||||||
* <p>A @Transactional annotation on a class specifies the default transaction |
* semantics for the execution of any <b>public</b> operation in the class. |
||||||
* semantics for the execution of any <b>public</b> operation in the class. |
* |
||||||
* |
* <p>A @Transactional annotation on a method within the class overrides the |
||||||
* <p>A @Transactional annotation on a method within the class overrides the |
* default transaction semantics given by the class annotation (if present). |
||||||
* default transaction semantics given by the class annotation (if present). |
* Any method may be annotated (regardless of visibility). |
||||||
* Any method may be annotated (regardless of visibility). |
* Annotating non-public methods directly is the only way |
||||||
* Annotating non-public methods directly is the only way |
* to get transaction demarcation for the execution of such operations. |
||||||
* to get transaction demarcation for the execution of such operations. |
* |
||||||
* |
* @author Rod Johnson |
||||||
* @author Rod Johnson |
* @author Ramnivas Laddad |
||||||
* @author Ramnivas Laddad |
* @author Adrian Colyer |
||||||
* @author Adrian Colyer |
* @since 2.0 |
||||||
* @since 2.0 |
* @see org.springframework.transaction.annotation.Transactional |
||||||
* @see org.springframework.transaction.annotation.Transactional |
*/ |
||||||
*/ |
public aspect AnnotationTransactionAspect extends AbstractTransactionAspect { |
||||||
public aspect AnnotationTransactionAspect extends AbstractTransactionAspect { |
|
||||||
|
public AnnotationTransactionAspect() { |
||||||
public AnnotationTransactionAspect() { |
super(new AnnotationTransactionAttributeSource(false)); |
||||||
super(new AnnotationTransactionAttributeSource(false)); |
} |
||||||
} |
|
||||||
|
/** |
||||||
/** |
* Matches the execution of any public method in a type with the |
||||||
* Matches the execution of any public method in a type with the |
* Transactional annotation, or any subtype of a type with the |
||||||
* Transactional annotation, or any subtype of a type with the |
* Transactional annotation. |
||||||
* Transactional annotation. |
*/ |
||||||
*/ |
private pointcut executionOfAnyPublicMethodInAtTransactionalType() : |
||||||
private pointcut executionOfAnyPublicMethodInAtTransactionalType() : |
execution(public * ((@Transactional *)+).*(..)) && @this(Transactional); |
||||||
execution(public * ((@Transactional *)+).*(..)) && @this(Transactional); |
|
||||||
|
/** |
||||||
/** |
* Matches the execution of any method with the |
||||||
* Matches the execution of any method with the |
* Transactional annotation. |
||||||
* Transactional annotation. |
*/ |
||||||
*/ |
private pointcut executionOfTransactionalMethod() : |
||||||
private pointcut executionOfTransactionalMethod() : |
execution(* *(..)) && @annotation(Transactional); |
||||||
execution(* *(..)) && @annotation(Transactional); |
|
||||||
|
/** |
||||||
/** |
* Definition of pointcut from super aspect - matched join points |
||||||
* Definition of pointcut from super aspect - matched join points |
* will have Spring transaction management applied. |
||||||
* will have Spring transaction management applied. |
*/ |
||||||
*/ |
protected pointcut transactionalMethodExecution(Object txObject) : |
||||||
protected pointcut transactionalMethodExecution(Object txObject) : |
(executionOfAnyPublicMethodInAtTransactionalType() |
||||||
(executionOfAnyPublicMethodInAtTransactionalType() |
|| executionOfTransactionalMethod() ) |
||||||
|| executionOfTransactionalMethod() ) |
&& this(txObject); |
||||||
&& this(txObject); |
|
||||||
|
} |
||||||
} |
|
||||||
|
|||||||
@ -1,17 +1,18 @@ |
|||||||
<?xml version="1.0"?> |
<?xml version="1.0"?> |
||||||
|
|
||||||
<!-- |
<!-- |
||||||
AspectJ load-time weaving config file to install common Spring aspects. |
AspectJ load-time weaving config file to install common Spring aspects. |
||||||
--> |
--> |
||||||
<aspectj> |
<aspectj> |
||||||
|
|
||||||
<!-- |
<!-- |
||||||
<weaver options="-showWeaveInfo"/> |
<weaver options="-showWeaveInfo"/> |
||||||
--> |
--> |
||||||
|
|
||||||
<aspects> |
<aspects> |
||||||
<aspect name="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"/> |
<aspect name="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"/> |
||||||
<aspect name="org.springframework.transaction.aspectj.AnnotationTransactionAspect"/> |
<aspect name="org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect"/> |
||||||
</aspects> |
<aspect name="org.springframework.transaction.aspectj.AnnotationTransactionAspect"/> |
||||||
|
</aspects> |
||||||
</aspectj> |
|
||||||
|
</aspectj> |
||||||
|
|||||||
@ -0,0 +1,22 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<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:task="http://www.springframework.org/schema/task" |
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
||||||
|
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd |
||||||
|
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> |
||||||
|
|
||||||
|
<!-- |
||||||
|
<context:load-time-weaver aspectj-weaving="on"/> |
||||||
|
--> |
||||||
|
|
||||||
|
<task:annotation-driven executor="executor"/> |
||||||
|
|
||||||
|
<bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> |
||||||
|
<property name="threadNamePrefix" value="testExecutor"/> |
||||||
|
</bean> |
||||||
|
|
||||||
|
<bean id="target" class="org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessorTests$TestBean"/> |
||||||
|
|
||||||
|
</beans> |
||||||
Loading…
Reference in new issue