Skip to content

Commit

Permalink
More precise type information of factory bean definitions.
Browse files Browse the repository at this point in the history
We now also forward the domain and identifier information detected on the repository to the target type declared for the repository factory bean definition.

Fixes GH-3074.
  • Loading branch information
odrotbohm committed Apr 9, 2024
1 parent 1c8471c commit dd081d4
Showing 1 changed file with 23 additions and 8 deletions.
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.repository.config;

import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -46,6 +47,8 @@
import org.springframework.core.log.LogMessage;
import org.springframework.core.metrics.ApplicationStartup;
import org.springframework.core.metrics.StartupStep;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.AbstractRepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -182,7 +185,6 @@ public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegist
}

RootBeanDefinition beanDefinition = (RootBeanDefinition) definitionBuilder.getBeanDefinition();

beanDefinition.setTargetType(getRepositoryInterface(configuration));
beanDefinition.setResourceDescription(configuration.getResourceDescription());

Expand All @@ -206,7 +208,7 @@ public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegist

if (logger.isInfoEnabled()) {
logger.info(
LogMessage.format("Finished Spring Data repository scanning in %s ms. Found %s %s repository interface%s." ,
LogMessage.format("Finished Spring Data repository scanning in %s ms. Found %s %s repository interface%s.",
watch.lastTaskInfo().getTimeMillis(), configurations.size(), extension.getModuleName(),
configurations.size() == 1 ? "" : "s"));
}
Expand Down Expand Up @@ -325,17 +327,30 @@ private ResolvableType getRepositoryInterface(RepositoryConfiguration<?> configu
classLoader = classLoader != null ? classLoader : getClass().getClassLoader();

Class<?> repositoryInterface = ReflectionUtils.loadIfPresent(interfaceName, classLoader);

if (repositoryInterface == null) {
return null;
}

Class<?> factoryBean = ReflectionUtils.loadIfPresent(configuration.getRepositoryFactoryBeanClassName(),
classLoader);

int numberOfGenerics = factoryBean.getTypeParameters().length;
if (factoryBean == null) {
return null;
}

TypeVariable<?>[] variables = factoryBean.getTypeParameters();
int numberOfGenerics = variables.length;
RepositoryMetadata metadata = AbstractRepositoryMetadata.getMetadata(repositoryInterface);

Class<?>[] generics = new Class<?>[numberOfGenerics];
generics[0] = repositoryInterface;
ResolvableType[] generics = new ResolvableType[numberOfGenerics];
generics[0] = ResolvableType.forClass(repositoryInterface);
generics[1] = ResolvableType.forClass(metadata.getDomainType());
generics[2] = ResolvableType.forClass(metadata.getIdType());

if (numberOfGenerics > 1) {
for (int i = 1; i < numberOfGenerics; i++) {
generics[i] = Object.class;
if (numberOfGenerics > 3) {
for (int i = 3; i < numberOfGenerics; i++) {
generics[i] = ResolvableType.forType(variables[0]);
}
}

Expand Down

0 comments on commit dd081d4

Please sign in to comment.