2121import org .apache .commons .logging .Log ;
2222import org .apache .commons .logging .LogFactory ;
2323
24+ import org .springframework .aop .scope .ScopedObject ;
2425import org .springframework .core .InfrastructureProxy ;
2526import org .springframework .util .Assert ;
26- import org .springframework .aop . scope . ScopedObject ;
27+ import org .springframework .util . ClassUtils ;
2728
2829/**
2930 * Utility methods for triggering specific {@link TransactionSynchronization}
@@ -38,6 +39,9 @@ public abstract class TransactionSynchronizationUtils {
3839
3940 private static final Log logger = LogFactory .getLog (TransactionSynchronizationUtils .class );
4041
42+ private static final boolean aopAvailable = ClassUtils .isPresent (
43+ "org.springframework.aop.scope.ScopedObject" , TransactionSynchronizationUtils .class .getClassLoader ());
44+
4145
4246 /**
4347 * Check whether the given resource transaction managers refers to the given
@@ -57,12 +61,15 @@ public static boolean sameResourceFactory(ResourceTransactionManager tm, Object
5761 static Object unwrapResourceIfNecessary (Object resource ) {
5862 Assert .notNull (resource , "Resource must not be null" );
5963 Object resourceRef = resource ;
60- if (resource instanceof ScopedObject ) {
61- // First unwrap a scoped proxy.
62- resourceRef = ((ScopedObject ) resource ).getTargetObject ();
64+ // unwrap infrastructure proxy
65+ if (resourceRef instanceof InfrastructureProxy ) {
66+ resourceRef = ((InfrastructureProxy ) resourceRef ).getWrappedObject ();
67+ }
68+ if (aopAvailable ) {
69+ // now unwrap scoped proxy
70+ resourceRef = ScopedProxyUnwrapper .unwrapIfNecessary (resource );
6371 }
64- // Now unwrap infrastructure proxy
65- return (resourceRef instanceof InfrastructureProxy ? ((InfrastructureProxy ) resourceRef ).getWrappedObject () : resourceRef );
72+ return resourceRef ;
6673 }
6774
6875
@@ -167,4 +174,20 @@ public static void invokeAfterCompletion(List<TransactionSynchronization> synchr
167174 }
168175 }
169176
177+
178+ /**
179+ * Inner class to avoid hard-coded dependency on AOP module.
180+ */
181+ private static class ScopedProxyUnwrapper {
182+
183+ public static Object unwrapIfNecessary (Object resource ) {
184+ if (resource instanceof ScopedObject ) {
185+ return ((ScopedObject ) resource ).getTargetObject ();
186+ }
187+ else {
188+ return resource ;
189+ }
190+ }
191+ }
192+
170193}
0 commit comments