Skip to content

Commit

Permalink
Set FACTORY_BEAN_OBJECT_TYPE as Class in RepositoryConfigurationDeleg…
Browse files Browse the repository at this point in the history
…ate.

This is to accommodate spring-projects/spring-framework#30917.
  • Loading branch information
odrotbohm committed Aug 1, 2023
1 parent c05ed09 commit d98eeef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
import org.springframework.core.metrics.ApplicationStartup;
import org.springframework.core.metrics.StartupStep;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StopWatch;

/**
Expand Down Expand Up @@ -183,7 +185,7 @@ public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegist

AbstractBeanDefinition beanDefinition = definitionBuilder.getBeanDefinition();

beanDefinition.setAttribute(FACTORY_BEAN_OBJECT_TYPE, configuration.getRepositoryInterface());
beanDefinition.setAttribute(FACTORY_BEAN_OBJECT_TYPE, getRepositoryInterface(configuration));
beanDefinition.setResourceDescription(configuration.getResourceDescription());

String beanName = configurationSource.generateBeanName(beanDefinition);
Expand Down Expand Up @@ -307,6 +309,23 @@ private static ApplicationStartup getStartup(BeanDefinitionRegistry registry) {
return ApplicationStartup.DEFAULT;
}

/**
* Returns the repository interface of the given {@link RepositoryConfiguration} as loaded {@link Class}.
*
* @param configuration must not be {@literal null}.
* @return can be {@literal null}.
*/
@Nullable
private Class<?> getRepositoryInterface(RepositoryConfiguration<?> configuration) {

String interfaceName = configuration.getRepositoryInterface();
ClassLoader classLoader = resourceLoader.getClassLoader() == null
? ClassUtils.getDefaultClassLoader()
: resourceLoader.getClassLoader();

return ReflectionUtils.loadIfPresent(interfaceName, classLoader);
}

/**
* Customer {@link ContextAnnotationAutowireCandidateResolver} that also considers all injection points for lazy
* repositories lazy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class RepositoryConfigurationDelegateUnitTests {

RepositoryConfigurationExtension extension = new DummyConfigurationExtension();

@Test // DATACMNS-892
@Test // DATACMNS-892, #2891
void registersRepositoryBeanNameAsAttribute() {

var environment = new StandardEnvironment();
Expand All @@ -76,8 +76,11 @@ void registersRepositoryBeanNameAsAttribute() {
for (var definition : delegate.registerRepositoriesIn(context, extension)) {

var beanDefinition = definition.getBeanDefinition();
var attribute = beanDefinition.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE);

assertThat(beanDefinition.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE).toString()).endsWith("Repository");
assertThat(attribute).isInstanceOfSatisfying(Class.class, it -> {
assertThat(it.getName()).endsWith("Repository");
});
}
}

Expand Down

0 comments on commit d98eeef

Please sign in to comment.