Skip to content

Commit dfa029b

Browse files
Guard transaction proxy creation hints.
Closes: #4225 Related: #4221
1 parent 04a8c47 commit dfa029b

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/aot/MongoAotPredicates.java

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.springframework.data.util.ReactiveWrappers;
2222
import org.springframework.data.util.ReactiveWrappers.ReactiveLibrary;
2323
import org.springframework.data.util.TypeUtils;
24+
import org.springframework.lang.Nullable;
25+
import org.springframework.util.ClassUtils;
2426

2527
/**
2628
* @author Christoph Strobl
@@ -30,9 +32,14 @@ public class MongoAotPredicates {
3032

3133
public static final Predicate<Class<?>> IS_SIMPLE_TYPE = (type) -> MongoSimpleTypes.HOLDER.isSimpleType(type) || TypeUtils.type(type).isPartOf("org.bson");
3234
public static final Predicate<ReactiveLibrary> IS_REACTIVE_LIBARARY_AVAILABLE = (lib) -> ReactiveWrappers.isAvailable(lib);
35+
public static final Predicate<ClassLoader> IS_SYNC_CLIENT_PRESENT = (classLoader) -> ClassUtils.isPresent("com.mongodb.client.MongoClient", classLoader);
3336

3437
public static boolean isReactorPresent() {
3538
return IS_REACTIVE_LIBARARY_AVAILABLE.test(ReactiveWrappers.ReactiveLibrary.PROJECT_REACTOR);
3639
}
3740

41+
public static boolean isSyncClientPresent(@Nullable ClassLoader classLoader) {
42+
return IS_SYNC_CLIENT_PRESENT.test(classLoader);
43+
}
44+
3845
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/aot/MongoRuntimeHints.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,7 @@ public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader)
5252
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
5353
MemberCategory.INVOKE_PUBLIC_METHODS));
5454

55-
if (ClassUtils.isPresent("org.springframework.aop.SpringProxy", classLoader)) {
56-
57-
hints.proxies().registerJdkProxy(TypeReference.of(com.mongodb.client.MongoDatabase.class),
58-
TypeReference.of("org.springframework.aop.SpringProxy"),
59-
TypeReference.of("org.springframework.core.DecoratingProxy"));
60-
hints.proxies().registerJdkProxy(TypeReference.of(com.mongodb.client.MongoCollection.class),
61-
TypeReference.of("org.springframework.aop.SpringProxy"),
62-
TypeReference.of("org.springframework.core.DecoratingProxy"));
63-
}
55+
registerTransactionProxyHints(hints, classLoader);
6456

6557
if (isReactorPresent()) {
6658

@@ -73,4 +65,17 @@ public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader)
7365

7466
}
7567
}
68+
69+
private static void registerTransactionProxyHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
70+
71+
if (MongoAotPredicates.isSyncClientPresent(classLoader) && ClassUtils.isPresent("org.springframework.aop.SpringProxy", classLoader)) {
72+
73+
hints.proxies().registerJdkProxy(TypeReference.of("com.mongodb.client.MongoDatabase"),
74+
TypeReference.of("org.springframework.aop.SpringProxy"),
75+
TypeReference.of("org.springframework.core.DecoratingProxy"));
76+
hints.proxies().registerJdkProxy(TypeReference.of("com.mongodb.client.MongoCollection"),
77+
TypeReference.of("org.springframework.aop.SpringProxy"),
78+
TypeReference.of("org.springframework.core.DecoratingProxy"));
79+
}
80+
}
7681
}

0 commit comments

Comments
 (0)