27
27
import org .jspecify .annotations .Nullable ;
28
28
29
29
import org .springframework .beans .BeanUtils ;
30
+ import org .springframework .beans .factory .config .RuntimeBeanReference ;
30
31
import org .springframework .core .annotation .AnnotatedElementUtils ;
31
32
import org .springframework .core .annotation .MergedAnnotation ;
32
33
import org .springframework .core .annotation .MergedAnnotations ;
@@ -120,7 +121,16 @@ protected void customizeClass(AotRepositoryClassBuilder classBuilder) {
120
121
@ Override
121
122
protected void customizeConstructor (AotRepositoryConstructorBuilder constructorBuilder ) {
122
123
123
- constructorBuilder .addParameter ("entityManager" , EntityManager .class );
124
+ String entityManagerFactoryRef = getEntityManagerFactoryRef ();
125
+
126
+ constructorBuilder .addParameter ("entityManager" , EntityManager .class , customizer -> {
127
+
128
+ customizer .bindToField ().origin (
129
+ StringUtils .hasText (entityManagerFactoryRef )
130
+ ? new RuntimeBeanReference (entityManagerFactoryRef , EntityManager .class )
131
+ : new RuntimeBeanReference (EntityManager .class ));
132
+ });
133
+
124
134
constructorBuilder .addParameter ("context" , RepositoryFactoryBeanSupport .FragmentCreationContext .class );
125
135
126
136
Optional <Class <QueryEnhancerSelector >> queryEnhancerSelector = getQueryEnhancerSelectorClass ();
@@ -135,6 +145,11 @@ protected void customizeConstructor(AotRepositoryConstructorBuilder constructorB
135
145
});
136
146
}
137
147
148
+ private String getEntityManagerFactoryRef () {
149
+ return context .getConfigurationSource ().getAttribute ("entityManagerFactoryRef" )
150
+ .filter (it -> !"entityManagerFactory" .equals (it )).orElse (null );
151
+ }
152
+
138
153
@ SuppressWarnings ({ "rawtypes" , "unchecked" })
139
154
private Optional <Class <QueryEnhancerSelector >> getQueryEnhancerSelectorClass () {
140
155
return (Optional ) context .getConfigurationSource ().getAttribute ("queryEnhancerSelector" , Class .class )
0 commit comments