21
21
import org .apache .commons .logging .Log ;
22
22
import org .apache .commons .logging .LogFactory ;
23
23
24
+ import org .springframework .aop .scope .ScopedObject ;
24
25
import org .springframework .core .InfrastructureProxy ;
25
26
import org .springframework .util .Assert ;
26
- import org .springframework .aop . scope . ScopedObject ;
27
+ import org .springframework .util . ClassUtils ;
27
28
28
29
/**
29
30
* Utility methods for triggering specific {@link TransactionSynchronization}
@@ -38,6 +39,9 @@ public abstract class TransactionSynchronizationUtils {
38
39
39
40
private static final Log logger = LogFactory .getLog (TransactionSynchronizationUtils .class );
40
41
42
+ private static final boolean aopAvailable = ClassUtils .isPresent (
43
+ "org.springframework.aop.scope.ScopedObject" , TransactionSynchronizationUtils .class .getClassLoader ());
44
+
41
45
42
46
/**
43
47
* Check whether the given resource transaction managers refers to the given
@@ -57,12 +61,15 @@ public static boolean sameResourceFactory(ResourceTransactionManager tm, Object
57
61
static Object unwrapResourceIfNecessary (Object resource ) {
58
62
Assert .notNull (resource , "Resource must not be null" );
59
63
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 );
63
71
}
64
- // Now unwrap infrastructure proxy
65
- return (resourceRef instanceof InfrastructureProxy ? ((InfrastructureProxy ) resourceRef ).getWrappedObject () : resourceRef );
72
+ return resourceRef ;
66
73
}
67
74
68
75
@@ -167,4 +174,20 @@ public static void invokeAfterCompletion(List<TransactionSynchronization> synchr
167
174
}
168
175
}
169
176
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
+
170
193
}
0 commit comments