Skip to content

Commit 125ebd0

Browse files
committed
Prevent AOT from failing with spring-orm without JPA
This commit improves PersistenceManagedTypesBeanRegistrationAotProcessor so that it does not attempt to load JPA classes when checking for the presence of a PersistenceManagedTypes bean. To make it more clear a check on the presence for JPA has been added to prevent the nested classes to be loaded regardless of the presence of the bean. Closes gh-32160
1 parent d8d4fa0 commit 125ebd0

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessor.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,34 @@
6767
*/
6868
class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
6969

70-
private static final List<Class<? extends Annotation>> CALLBACK_TYPES = List.of(PreUpdate.class,
71-
PostUpdate.class, PrePersist.class, PostPersist.class, PreRemove.class, PostRemove.class, PostLoad.class);
70+
private static final boolean jpaPresent = ClassUtils.isPresent("jakarta.persistence.Entity",
71+
PersistenceManagedTypesBeanRegistrationAotProcessor.class.getClassLoader());
7272

7373
@Nullable
7474
@Override
7575
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
76-
if (PersistenceManagedTypes.class.isAssignableFrom(registeredBean.getBeanClass())) {
77-
return BeanRegistrationAotContribution.withCustomCodeFragments(codeFragments ->
78-
new JpaManagedTypesBeanRegistrationCodeFragments(codeFragments, registeredBean));
76+
if (jpaPresent) {
77+
if (PersistenceManagedTypes.class.isAssignableFrom(registeredBean.getBeanClass())) {
78+
return BeanRegistrationAotContribution.withCustomCodeFragments(codeFragments ->
79+
new JpaManagedTypesBeanRegistrationCodeFragments(codeFragments, registeredBean));
80+
}
7981
}
8082
return null;
8183
}
8284

83-
private static class JpaManagedTypesBeanRegistrationCodeFragments extends BeanRegistrationCodeFragmentsDecorator {
85+
private static final class JpaManagedTypesBeanRegistrationCodeFragments extends BeanRegistrationCodeFragmentsDecorator {
86+
87+
private static final List<Class<? extends Annotation>> CALLBACK_TYPES = List.of(PreUpdate.class,
88+
PostUpdate.class, PrePersist.class, PostPersist.class, PreRemove.class, PostRemove.class, PostLoad.class);
89+
8490

8591
private static final ParameterizedTypeName LIST_OF_STRINGS_TYPE = ParameterizedTypeName.get(List.class, String.class);
8692

8793
private final RegisteredBean registeredBean;
8894

8995
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
9096

91-
public JpaManagedTypesBeanRegistrationCodeFragments(BeanRegistrationCodeFragments codeFragments,
97+
private JpaManagedTypesBeanRegistrationCodeFragments(BeanRegistrationCodeFragments codeFragments,
9298
RegisteredBean registeredBean) {
9399
super(codeFragments);
94100
this.registeredBean = registeredBean;

0 commit comments

Comments
 (0)