DATAMONGO-1291 - Made @Document usable as meta-annotation.

We now use Spring's AnnotationUtils.findAnnotation(…) for @Document lookup which enables the full power of Spring 4.2's composable annotations.

Original pull request: #326.
This commit is contained in:
Christoph Strobl
2015-09-22 08:47:20 +02:00
committed by Oliver Gierke
parent f8416edf8f
commit ddc3925659
2 changed files with 30 additions and 1 deletions

View File

@@ -26,6 +26,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.expression.BeanFactoryAccessor;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.AssociationHandler;
@@ -77,7 +78,7 @@ public class BasicMongoPersistentEntity<T> extends BasicPersistentEntity<T, Mong
Class<?> rawType = typeInformation.getType();
String fallback = MongoCollectionUtils.getPreferredCollectionName(rawType);
Document document = rawType.getAnnotation(Document.class);
Document document = AnnotationUtils.findAnnotation(rawType, Document.class);
this.expression = detectExpression(document);
this.context = new StandardEvaluationContext();

View File

@@ -19,6 +19,11 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -226,6 +231,18 @@ public class BasicMongoPersistentEntityUnitTests {
verify(dbRefMock, times(1)).lazy();
}
/**
* @see DATAMONGO-1291
*/
@Test
public void metaInformationShouldBeReadCorrectlyFromInheritedDocumentAnnotation() {
BasicMongoPersistentEntity<DocumentWithCustomAnnotation> entity = new BasicMongoPersistentEntity<DocumentWithCustomAnnotation>(
ClassTypeInformation.from(DocumentWithCustomAnnotation.class));
assertThat(entity.getCollection(), is("collection-1"));
}
@Document(collection = "contacts")
class Contact {
@@ -261,4 +278,15 @@ public class BasicMongoPersistentEntityUnitTests {
static class AnyDocument {
}
@CustomDocumentAnnotation
static class DocumentWithCustomAnnotation {
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
@Document(collection = "collection-1")
static @interface CustomDocumentAnnotation {
}
}