2525
2626import org .apache .commons .logging .Log ;
2727import org .apache .commons .logging .LogFactory ;
28- import org .springframework .beans .factory .FactoryBean ;
2928import org .springframework .beans .factory .config .BeanDefinition ;
3029import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
3130import org .springframework .beans .factory .config .DependencyDescriptor ;
3231import org .springframework .beans .factory .parsing .BeanComponentDefinition ;
33- import org .springframework .beans .factory .support .AbstractBeanDefinition ;
3432import org .springframework .beans .factory .support .AutowireCandidateResolver ;
3533import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
3634import org .springframework .beans .factory .support .BeanDefinitionReaderUtils ;
3735import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
3836import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
37+ import org .springframework .beans .factory .support .RootBeanDefinition ;
3938import org .springframework .context .annotation .ContextAnnotationAutowireCandidateResolver ;
4039import org .springframework .context .support .GenericApplicationContext ;
40+ import org .springframework .core .ResolvableType ;
4141import org .springframework .core .env .Environment ;
4242import org .springframework .core .env .EnvironmentCapable ;
4343import org .springframework .core .env .StandardEnvironment ;
4646import org .springframework .core .log .LogMessage ;
4747import org .springframework .core .metrics .ApplicationStartup ;
4848import org .springframework .core .metrics .StartupStep ;
49+ import org .springframework .data .repository .core .RepositoryMetadata ;
50+ import org .springframework .data .repository .core .support .AbstractRepositoryMetadata ;
4951import org .springframework .data .repository .core .support .RepositoryFactorySupport ;
5052import org .springframework .data .util .ReflectionUtils ;
5153import org .springframework .lang .Nullable ;
@@ -70,7 +72,6 @@ public class RepositoryConfigurationDelegate {
7072 private static final String REPOSITORY_REGISTRATION = "Spring Data %s - Registering repository: %s - Interface: %s - Factory: %s" ;
7173 private static final String MULTIPLE_MODULES = "Multiple Spring Data modules found, entering strict repository configuration mode" ;
7274 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 ;
7475
7576 private static final Log logger = LogFactory .getLog (RepositoryConfigurationDelegate .class );
7677
@@ -183,9 +184,9 @@ public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegist
183184 extension .postProcess (definitionBuilder , (AnnotationRepositoryConfigurationSource ) configurationSource );
184185 }
185186
186- AbstractBeanDefinition beanDefinition = definitionBuilder .getBeanDefinition ();
187+ RootBeanDefinition beanDefinition = ( RootBeanDefinition ) definitionBuilder .getBeanDefinition ();
187188
188- beanDefinition .setAttribute ( FACTORY_BEAN_OBJECT_TYPE , getRepositoryInterface (configuration ));
189+ beanDefinition .setTargetType ( getRepositoryInterface (configuration ));
189190 beanDefinition .setResourceDescription (configuration .getResourceDescription ());
190191
191192 String beanName = configurationSource .generateBeanName (beanDefinition );
@@ -316,14 +317,34 @@ private static ApplicationStartup getStartup(BeanDefinitionRegistry registry) {
316317 * @return can be {@literal null}.
317318 */
318319 @ Nullable
319- private Class <?> getRepositoryInterface (RepositoryConfiguration <?> configuration ) {
320+ private ResolvableType getRepositoryInterface (RepositoryConfiguration <?> configuration ) {
320321
321322 String interfaceName = configuration .getRepositoryInterface ();
322323 ClassLoader classLoader = resourceLoader .getClassLoader () == null
323324 ? ClassUtils .getDefaultClassLoader ()
324325 : resourceLoader .getClassLoader ();
325326
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 );
327348 }
328349
329350 /**
0 commit comments