34
34
35
35
import org .aopalliance .intercept .MethodInterceptor ;
36
36
import org .aopalliance .intercept .MethodInvocation ;
37
+ import org .jetbrains .annotations .NotNull ;
37
38
import org .springframework .aop .framework .ProxyFactory ;
38
39
import org .springframework .aop .interceptor .ExposeInvocationInterceptor ;
39
40
import org .springframework .beans .BeanUtils ;
45
46
import org .springframework .core .ResolvableType ;
46
47
import org .springframework .core .convert .TypeDescriptor ;
47
48
import org .springframework .data .projection .DefaultMethodInvokingMethodInterceptor ;
49
+ import org .springframework .data .projection .ProjectionFactory ;
48
50
import org .springframework .data .projection .SpelAwareProxyProjectionFactory ;
49
51
import org .springframework .data .repository .Repository ;
50
52
import org .springframework .data .repository .core .EntityInformation ;
77
79
* @author Oliver Gierke
78
80
* @author Mark Paluch
79
81
* @author Christoph Strobl
82
+ * @author Jens Schauder
80
83
*/
81
84
public abstract class RepositoryFactorySupport implements BeanClassLoaderAware , BeanFactoryAware {
82
85
@@ -312,14 +315,27 @@ public <T> T getRepository(Class<T> repositoryInterface, RepositoryFragments fra
312
315
postProcessors .forEach (processor -> processor .postProcess (result , information ));
313
316
314
317
result .addAdvice (new DefaultMethodInvokingMethodInterceptor ());
315
- result .addAdvice (new QueryExecutorMethodInterceptor (information ));
318
+ result .addAdvice (new QueryExecutorMethodInterceptor ( //
319
+ information , //
320
+ getProjectionFactory (classLoader , beanFactory ) //
321
+ ));
316
322
317
323
composition = composition .append (RepositoryFragment .implemented (target ));
318
324
result .addAdvice (new ImplementationMethodExecutionInterceptor (composition ));
319
325
320
326
return (T ) result .getProxy (classLoader );
321
327
}
322
328
329
+ @ NotNull
330
+ protected ProjectionFactory getProjectionFactory (ClassLoader classLoader , BeanFactory beanFactory ) {
331
+
332
+ SpelAwareProxyProjectionFactory factory = new SpelAwareProxyProjectionFactory ();
333
+ factory .setBeanClassLoader (classLoader );
334
+ factory .setBeanFactory (beanFactory );
335
+
336
+ return factory ;
337
+ }
338
+
323
339
/**
324
340
* Returns the {@link RepositoryMetadata} for the given repository interface.
325
341
*
@@ -501,7 +517,8 @@ public class QueryExecutorMethodInterceptor implements MethodInterceptor {
501
517
* Creates a new {@link QueryExecutorMethodInterceptor}. Builds a model of {@link QueryMethod}s to be invoked on
502
518
* execution of repository interface methods.
503
519
*/
504
- public QueryExecutorMethodInterceptor (RepositoryInformation repositoryInformation ) {
520
+ public QueryExecutorMethodInterceptor (RepositoryInformation repositoryInformation ,
521
+ ProjectionFactory projectionFactory ) {
505
522
506
523
this .resultHandler = new QueryExecutionResultHandler ();
507
524
@@ -515,18 +532,20 @@ public QueryExecutorMethodInterceptor(RepositoryInformation repositoryInformatio
515
532
+ "infrastructure apparently does not support query methods!" );
516
533
}
517
534
518
- this .queries = lookupStrategy .map (it -> {
519
-
520
- SpelAwareProxyProjectionFactory factory = new SpelAwareProxyProjectionFactory ();
521
- factory .setBeanClassLoader (classLoader );
522
- factory .setBeanFactory (beanFactory );
535
+ this .queries = lookupStrategy .map ( //
536
+ it -> mapMethodsToQuery (repositoryInformation , projectionFactory , it ) //
537
+ ).orElse (Collections .emptyMap ());
538
+ }
523
539
524
- return repositoryInformation .getQueryMethods ().stream ()//
525
- .map (method -> Pair .of (method , it .resolveQuery (method , repositoryInformation , factory , namedQueries )))//
526
- .peek (pair -> invokeListeners (pair .getSecond ()))//
527
- .collect (Pair .toMap ());
540
+ private Map <Method , RepositoryQuery > mapMethodsToQuery (RepositoryInformation repositoryInformation ,
541
+ ProjectionFactory projectionFactory , QueryLookupStrategy lookupStrategy ) {
528
542
529
- }).orElse (Collections .emptyMap ());
543
+ return repositoryInformation .getQueryMethods ().stream () //
544
+ .map (method -> Pair .of ( //
545
+ method , //
546
+ lookupStrategy .resolveQuery (method , repositoryInformation , projectionFactory , namedQueries ))) //
547
+ .peek (pair -> invokeListeners (pair .getSecond ())) //
548
+ .collect (Pair .toMap ());
530
549
}
531
550
532
551
@ SuppressWarnings ({ "rawtypes" , "unchecked" })
0 commit comments