DATACMNS-392 - Support for eager repository instantiation.

Changed the initialization of repositories to default to eager with the ability to explicitly defer instantiation using @Lazy on the repository interface definition. In practice, the change in default shouldn't make any difference to most client projects as repositories are usually depended on by other application components so that the repositories never stay uninitialized.
This commit is contained in:
Oliver Gierke
2013-10-27 16:21:24 +01:00
parent d26f90b281
commit e616ec5d05
11 changed files with 131 additions and 32 deletions

View File

@@ -22,6 +22,7 @@ import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader;
@@ -57,9 +58,11 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
@Test
public void evaluatesExcludeFiltersCorrectly() {
Collection<String> candidates = source.getCandidates(new DefaultResourceLoader());
Collection<BeanDefinition> candidates = source.getCandidates(new DefaultResourceLoader());
assertThat(candidates, hasSize(1));
assertThat(candidates, hasItem(MyRepository.class.getName()));
BeanDefinition candidate = candidates.iterator().next();
assertThat(candidate.getBeanClassName(), is(MyRepository.class.getName()));
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
/**
@@ -32,19 +33,19 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key;
@RunWith(MockitoJUnitRunner.class)
public class DefaultRepositoryConfigurationUnitTests {
@Mock
RepositoryConfigurationSource source;
@Mock RepositoryConfigurationSource source;
@Test
public void supportsBasicConfiguration() {
RepositoryConfiguration<RepositoryConfigurationSource> configuration = new DefaultRepositoryConfiguration<RepositoryConfigurationSource>(
source, "com.acme.MyRepository");
source, new RootBeanDefinition("com.acme.MyRepository"));
assertThat(configuration.getConfigurationSource(), is(source));
assertThat(configuration.getImplementationBeanName(), is("myRepositoryImpl"));
assertThat(configuration.getImplementationClassName(), is("MyRepositoryImpl"));
assertThat(configuration.getRepositoryInterface(), is("com.acme.MyRepository"));
assertThat(configuration.getQueryLookupStrategyKey(), is((Object) Key.CREATE_IF_NOT_FOUND));
assertThat(configuration.isLazyInit(), is(false));
}
}

View File

@@ -19,7 +19,9 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.test.util.ReflectionTestUtils;
/**
@@ -29,6 +31,8 @@ import org.springframework.test.util.ReflectionTestUtils;
*/
public class RepositoryFactoryBeanSupportUnitTests {
public @Rule ExpectedException exception = ExpectedException.none();
/**
* @see DATACMNS-341
*/
@@ -40,9 +44,21 @@ public class RepositoryFactoryBeanSupportUnitTests {
RepositoryFactoryBeanSupport factoryBean = new DummyRepositoryFactoryBean();
factoryBean.setBeanClassLoader(classLoader);
factoryBean.setLazyInit(true);
factoryBean.afterPropertiesSet();
Object factory = ReflectionTestUtils.getField(factoryBean, "factory");
assertThat(ReflectionTestUtils.getField(factory, "classLoader"), is((Object) classLoader));
}
@Test
@SuppressWarnings("rawtypes")
public void initializationFailsWithMissingRepositoryInterface() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("Repository interface");
RepositoryFactoryBeanSupport factoryBean = new DummyRepositoryFactoryBean();
factoryBean.afterPropertiesSet();
}
}