25
25
26
26
import org .apache .commons .logging .Log ;
27
27
import org .apache .commons .logging .LogFactory ;
28
- import org .springframework .beans .factory .FactoryBean ;
29
28
import org .springframework .beans .factory .config .BeanDefinition ;
30
29
import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
31
30
import org .springframework .beans .factory .config .DependencyDescriptor ;
32
31
import org .springframework .beans .factory .parsing .BeanComponentDefinition ;
33
- import org .springframework .beans .factory .support .AbstractBeanDefinition ;
34
32
import org .springframework .beans .factory .support .AutowireCandidateResolver ;
35
33
import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
36
34
import org .springframework .beans .factory .support .BeanDefinitionReaderUtils ;
37
35
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
38
36
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
37
+ import org .springframework .beans .factory .support .RootBeanDefinition ;
39
38
import org .springframework .context .annotation .ContextAnnotationAutowireCandidateResolver ;
40
39
import org .springframework .context .support .GenericApplicationContext ;
40
+ import org .springframework .core .ResolvableType ;
41
41
import org .springframework .core .env .Environment ;
42
42
import org .springframework .core .env .EnvironmentCapable ;
43
43
import org .springframework .core .env .StandardEnvironment ;
46
46
import org .springframework .core .log .LogMessage ;
47
47
import org .springframework .core .metrics .ApplicationStartup ;
48
48
import org .springframework .core .metrics .StartupStep ;
49
+ import org .springframework .data .repository .core .RepositoryMetadata ;
50
+ import org .springframework .data .repository .core .support .AbstractRepositoryMetadata ;
49
51
import org .springframework .data .repository .core .support .RepositoryFactorySupport ;
50
52
import org .springframework .data .util .ReflectionUtils ;
51
53
import org .springframework .lang .Nullable ;
@@ -70,7 +72,6 @@ public class RepositoryConfigurationDelegate {
70
72
private static final String REPOSITORY_REGISTRATION = "Spring Data %s - Registering repository: %s - Interface: %s - Factory: %s" ;
71
73
private static final String MULTIPLE_MODULES = "Multiple Spring Data modules found, entering strict repository configuration mode" ;
72
74
private static final String NON_DEFAULT_AUTOWIRE_CANDIDATE_RESOLVER = "Non-default AutowireCandidateResolver (%s) detected. Skipping the registration of LazyRepositoryInjectionPointResolver. Lazy repository injection will not be working" ;
73
- private static final String FACTORY_BEAN_OBJECT_TYPE = FactoryBean .OBJECT_TYPE_ATTRIBUTE ;
74
75
75
76
private static final Log logger = LogFactory .getLog (RepositoryConfigurationDelegate .class );
76
77
@@ -183,9 +184,9 @@ public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegist
183
184
extension .postProcess (definitionBuilder , (AnnotationRepositoryConfigurationSource ) configurationSource );
184
185
}
185
186
186
- AbstractBeanDefinition beanDefinition = definitionBuilder .getBeanDefinition ();
187
+ RootBeanDefinition beanDefinition = ( RootBeanDefinition ) definitionBuilder .getBeanDefinition ();
187
188
188
- beanDefinition .setAttribute ( FACTORY_BEAN_OBJECT_TYPE , getRepositoryInterface (configuration ));
189
+ beanDefinition .setTargetType ( getRepositoryInterface (configuration ));
189
190
beanDefinition .setResourceDescription (configuration .getResourceDescription ());
190
191
191
192
String beanName = configurationSource .generateBeanName (beanDefinition );
@@ -316,14 +317,34 @@ private static ApplicationStartup getStartup(BeanDefinitionRegistry registry) {
316
317
* @return can be {@literal null}.
317
318
*/
318
319
@ Nullable
319
- private Class <?> getRepositoryInterface (RepositoryConfiguration <?> configuration ) {
320
+ private ResolvableType getRepositoryInterface (RepositoryConfiguration <?> configuration ) {
320
321
321
322
String interfaceName = configuration .getRepositoryInterface ();
322
323
ClassLoader classLoader = resourceLoader .getClassLoader () == null
323
324
? ClassUtils .getDefaultClassLoader ()
324
325
: resourceLoader .getClassLoader ();
325
326
326
- return ReflectionUtils .loadIfPresent (interfaceName , classLoader );
327
+ classLoader = classLoader != null ? classLoader : getClass ().getClassLoader ();
328
+
329
+ Class <?> repositoryInterface = ReflectionUtils .loadIfPresent (interfaceName , classLoader );
330
+ Class <?> factoryBean = ReflectionUtils .loadIfPresent (configuration .getRepositoryFactoryBeanClassName (),
331
+ classLoader );
332
+ RepositoryMetadata metadata = AbstractRepositoryMetadata .getMetadata (repositoryInterface );
333
+
334
+ int numberOfGenerics = factoryBean .getTypeParameters ().length ;
335
+
336
+ Class <?>[] generics = new Class <?>[numberOfGenerics ];
337
+ generics [0 ] = metadata .getRepositoryInterface ();
338
+ generics [1 ] = metadata .getDomainType ();
339
+ generics [2 ] = metadata .getIdType ();
340
+
341
+ if (numberOfGenerics > 3 ) {
342
+ for (int i = 3 ; i < numberOfGenerics ; i ++) {
343
+ generics [i ] = Object .class ;
344
+ }
345
+ }
346
+
347
+ return ResolvableType .forClassWithGenerics (factoryBean , generics );
327
348
}
328
349
329
350
/**
0 commit comments