DATACMNS-1620 - Repository bean definitions now contain human readable resource descriptions.
We now expose human readable resource descriptions based on RepositoryConfiguration and RepositoryConfigurationSource. Spring Boot uses those to describe problematic bean definitions in case it detects errors in the application setup.
This commit is contained in:
@@ -321,6 +321,19 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationSource#getResourceDescription()
|
||||
*/
|
||||
@Override
|
||||
public String getResourceDescription() {
|
||||
|
||||
String simpleClassName = ClassUtils.getShortName(configMetadata.getClassName());
|
||||
String annoationClassName = ClassUtils.getShortName(enableAnnotationMetadata.getClassName());
|
||||
|
||||
return String.format("@%s declared on %s", annoationClassName, simpleClassName);
|
||||
}
|
||||
|
||||
private Streamable<TypeFilter> parseFilters(String attributeName) {
|
||||
|
||||
AnnotationAttributes[] filters = attributes.getAnnotationArray(attributeName);
|
||||
|
||||
@@ -214,4 +214,14 @@ public class DefaultRepositoryConfiguration<T extends RepositoryConfigurationSou
|
||||
|
||||
return toImplementationDetectionConfiguration(factory).forRepositoryConfiguration(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfiguration#getResourceDescription()
|
||||
*/
|
||||
@Override
|
||||
@org.springframework.lang.NonNull
|
||||
public String getResourceDescription() {
|
||||
return String.format("%s defined in %s", getRepositoryInterface(), configurationSource.getResourceDescription());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,4 +139,12 @@ public interface RepositoryConfiguration<T extends RepositoryConfigurationSource
|
||||
*/
|
||||
ImplementationLookupConfiguration toLookupConfiguration(MetadataReaderFactory factory);
|
||||
|
||||
/**
|
||||
* Returns a human readable description of the repository interface declaration for error reporting purposes.
|
||||
*
|
||||
* @return can be {@literal null}.
|
||||
* @since 2.3
|
||||
*/
|
||||
@Nullable
|
||||
String getResourceDescription();
|
||||
}
|
||||
|
||||
@@ -165,6 +165,7 @@ public class RepositoryConfigurationDelegate {
|
||||
|
||||
AbstractBeanDefinition beanDefinition = definitionBuilder.getBeanDefinition();
|
||||
beanDefinition.setPrimary(configuration.isPrimary());
|
||||
beanDefinition.setResourceDescription(configuration.getResourceDescription());
|
||||
|
||||
String beanName = configurationSource.generateBeanName(beanDefinition);
|
||||
|
||||
|
||||
@@ -171,4 +171,13 @@ public interface RepositoryConfigurationSource {
|
||||
* @since 2.1
|
||||
*/
|
||||
BootstrapMode getBootstrapMode();
|
||||
|
||||
/**
|
||||
* Returns a human readable description of the repository configuration source for error reporting purposes.
|
||||
*
|
||||
* @return can be {@literal null}.
|
||||
* @since 2.3
|
||||
*/
|
||||
@Nullable
|
||||
String getResourceDescription();
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.springframework.data.config.TypeFilterParser.Type;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
import org.springframework.data.util.ParsingUtils;
|
||||
import org.springframework.data.util.Streamable;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -244,6 +245,19 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou
|
||||
: BootstrapMode.DEFAULT;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationSource#getResourceDescription()
|
||||
*/
|
||||
@Override
|
||||
@NonNull
|
||||
public String getResourceDescription() {
|
||||
|
||||
Object source = getSource();
|
||||
|
||||
return source == null ? "" : source.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link BeanNameGenerator} to use falling back to an {@link AnnotationBeanNameGenerator} if either the
|
||||
* given generator is {@literal null} or it's {@link DefaultBeanNameGenerator} in particular. This is to make sure we
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupportUnitTests.DummyConfigurationExtension;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link RepositoryBeanDefinitionRegistrarSupport}.
|
||||
@@ -91,4 +92,15 @@ public class RepositoryBeanDefinitionRegistrarSupportIntegrationTests {
|
||||
public void composedRepositoriesShouldBeAssembledCorrectly() {
|
||||
assertThat(context.getBean(ComposedRepository.class).getOne()).isEqualTo("one");
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1620
|
||||
public void registeredBeanDefinitionsContainHumanReadableResourceDescription() {
|
||||
|
||||
BeanDefinition definition = context.getBeanDefinition("myRepository");
|
||||
|
||||
assertThat(definition.getResourceDescription()) //
|
||||
.contains(MyRepository.class.getName()) //
|
||||
.contains(EnableRepositories.class.getSimpleName()) //
|
||||
.contains(ClassUtils.getShortName(SampleConfig.class));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user