DATACMNS-891 - Moved to constructor injection for RepositoryFactoryBeanSupport.
RepositoryFactoryBeanSupport now takes the repository interface as a constructor argument instead of as a setter. This makes sure that the container induced type prediction for factory beans actually wires the interface *before* it calls getObjectType() on the instance. This allows us to remove the extra infrastructure we had in place to predict the bean types and autowiring will just work out of the box. Adapted infrastructure code accordingly, removed obsolete infrastructure code and adapted test cases accordingly.
This commit is contained in:
@@ -62,7 +62,7 @@ public class RepositoryBeanNameGeneratorUnitTests {
|
||||
private BeanDefinition getBeanDefinitionFor(Class<?> repositoryInterface) {
|
||||
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(RepositoryFactoryBeanSupport.class);
|
||||
builder.addPropertyValue("repositoryInterface", repositoryInterface.getName());
|
||||
builder.addConstructorArgValue(repositoryInterface.getName());
|
||||
return builder.getBeanDefinition();
|
||||
}
|
||||
|
||||
|
||||
@@ -23,12 +23,7 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.core.type.StandardAnnotationMetadata;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.AbstractRepositoryMetadata;
|
||||
@@ -73,26 +68,6 @@ public class RepositoryConfigurationExtensionSupportUnitTests {
|
||||
assertThat(extension.isStrictRepositoryCandidate(metadata), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-609
|
||||
*/
|
||||
@Test
|
||||
public void registersRepositoryInterfaceAwareBeanPostProcessorOnlyOnceForMultipleConfigurations() {
|
||||
|
||||
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
|
||||
AnnotationMetadata annotationMetadata = new StandardAnnotationMetadata(SampleConfiguration.class, true);
|
||||
|
||||
DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
|
||||
StandardEnvironment environment = new StandardEnvironment();
|
||||
AnnotationRepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(
|
||||
annotationMetadata, EnableRepositories.class, resourceLoader, environment);
|
||||
|
||||
extension.registerBeansForRoot(beanFactory, configurationSource);
|
||||
extension.registerBeansForRoot(beanFactory, configurationSource);
|
||||
|
||||
assertThat(beanFactory.getBeanDefinitionCount(), is(1));
|
||||
}
|
||||
|
||||
static class SampleRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2016 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.
|
||||
@@ -25,22 +25,17 @@ import org.springframework.data.repository.Repository;
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class DummyRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends
|
||||
RepositoryFactoryBeanSupport<T, S, ID> {
|
||||
public class DummyRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
|
||||
extends RepositoryFactoryBeanSupport<T, S, ID> {
|
||||
|
||||
private T repository;
|
||||
|
||||
public DummyRepositoryFactoryBean() {
|
||||
setMappingContext(new SampleMappingContext());
|
||||
}
|
||||
public DummyRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
|
||||
|
||||
super(repositoryInterface);
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport#setRepositoryInterface(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public void setRepositoryInterface(Class<? extends T> repositoryInterface) {
|
||||
this.repository = mock(repositoryInterface);
|
||||
super.setRepositoryInterface(repositoryInterface);
|
||||
setMappingContext(new SampleMappingContext());
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.repository.core.support;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.data.querydsl.User;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
/**
|
||||
* Integration test to make sure Spring Data repository factory beans are found without the factories already
|
||||
* instantiated.
|
||||
*
|
||||
* @see SPR-10517
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class FactoryBeanTypePredictingPostProcessorIntegrationTests {
|
||||
|
||||
DefaultListableBeanFactory factory;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
factory = new DefaultListableBeanFactory();
|
||||
|
||||
// Register factory bean for repository
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class);
|
||||
builder.addPropertyValue("repositoryInterface", UserRepository.class);
|
||||
factory.registerBeanDefinition("repository", builder.getBeanDefinition());
|
||||
|
||||
// Register predicting BeanPostProcessor
|
||||
FactoryBeanTypePredictingBeanPostProcessor processor = new FactoryBeanTypePredictingBeanPostProcessor(
|
||||
RepositoryFactoryBeanSupport.class, "repositoryInterface");
|
||||
processor.setBeanFactory(factory);
|
||||
factory.addBeanPostProcessor(processor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lookupBeforeInstantiation() {
|
||||
|
||||
String[] strings = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, RepositoryFactoryInformation.class,
|
||||
false, false);
|
||||
assertThat(Arrays.asList(strings), hasItem("&repository"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lookupAfterInstantiation() {
|
||||
|
||||
factory.getBean(UserRepository.class);
|
||||
|
||||
String[] strings = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, RepositoryFactoryInformation.class,
|
||||
false, false);
|
||||
assertThat(Arrays.asList(strings), hasItem("&repository"));
|
||||
}
|
||||
|
||||
interface UserRepository extends Repository<User, Long> {}
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
/*
|
||||
* Copyright 2008-2016 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. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package org.springframework.data.repository.core.support;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.jndi.JndiObjectFactoryBean;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link RepositoryInterfaceAwareBeanPostProcessor}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class FactoryBeanTypePredictingPostProcessorUnitTests {
|
||||
|
||||
private static final Class<?> FACTORY_CLASS = RepositoryFactoryBeanSupport.class;
|
||||
private static final String BEAN_NAME = "foo";
|
||||
private static final String DAO_INTERFACE_PROPERTY = "repositoryInterface";
|
||||
|
||||
FactoryBeanTypePredictingBeanPostProcessor processor;
|
||||
BeanDefinition beanDefinition;
|
||||
|
||||
@Mock ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(FACTORY_CLASS)
|
||||
.addPropertyValue(DAO_INTERFACE_PROPERTY, UserDao.class);
|
||||
this.beanDefinition = builder.getBeanDefinition();
|
||||
this.processor = new FactoryBeanTypePredictingBeanPostProcessor(FACTORY_CLASS, "repositoryInterface");
|
||||
|
||||
when(beanFactory.getBeanDefinition(BEAN_NAME)).thenReturn(beanDefinition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsDaoInterfaceClassForFactoryBean() throws Exception {
|
||||
|
||||
processor.setBeanFactory(beanFactory);
|
||||
assertEquals(UserDao.class, processor.predictBeanType(FACTORY_CLASS, BEAN_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotResolveInterfaceForNonFactoryClasses() throws Exception {
|
||||
|
||||
processor.setBeanFactory(beanFactory);
|
||||
assertNotTypeDetected(BeanFactory.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotResolveInterfaceForUnloadableClass() throws Exception {
|
||||
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(FACTORY_CLASS)
|
||||
.addPropertyValue(DAO_INTERFACE_PROPERTY, "com.acme.Foo");
|
||||
|
||||
when(beanFactory.getBeanDefinition(BEAN_NAME)).thenReturn(builder.getBeanDefinition());
|
||||
|
||||
assertNotTypeDetected(FACTORY_CLASS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotResolveTypeForPlainBeanFactory() throws Exception {
|
||||
|
||||
BeanFactory beanFactory = mock(BeanFactory.class);
|
||||
processor.setBeanFactory(beanFactory);
|
||||
|
||||
assertNotTypeDetected(FACTORY_CLASS);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNonFactoryBeanType() {
|
||||
new FactoryBeanTypePredictingBeanPostProcessor(Object.class, "property");
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-821
|
||||
*/
|
||||
@Test
|
||||
public void usesFirstValueIfPropertyIsOfArrayType() {
|
||||
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(JndiObjectFactoryBean.class);
|
||||
builder.addPropertyValue("proxyInterfaces",
|
||||
new String[] { Serializable.class.getName(), Iterable.class.getName() });
|
||||
|
||||
when(beanFactory.getBeanDefinition(BEAN_NAME)).thenReturn(builder.getBeanDefinition());
|
||||
|
||||
processor = new FactoryBeanTypePredictingBeanPostProcessor(JndiObjectFactoryBean.class, "proxyInterface",
|
||||
"proxyInterfaces");
|
||||
processor.setBeanFactory(beanFactory);
|
||||
|
||||
assertThat(processor.predictBeanType(JndiObjectFactoryBean.class, BEAN_NAME),
|
||||
is(typeCompatibleWith(Serializable.class)));
|
||||
}
|
||||
|
||||
private void assertNotTypeDetected(Class<?> beanClass) {
|
||||
assertThat(processor.predictBeanType(beanClass, BEAN_NAME), is(nullValue()));
|
||||
}
|
||||
|
||||
private class User {}
|
||||
|
||||
private interface UserDao extends Repository<User, Long> {}
|
||||
}
|
||||
@@ -44,10 +44,9 @@ public class RepositoryFactoryBeanSupportUnitTests {
|
||||
|
||||
ClassLoader classLoader = mock(ClassLoader.class);
|
||||
|
||||
RepositoryFactoryBeanSupport factoryBean = new DummyRepositoryFactoryBean();
|
||||
RepositoryFactoryBeanSupport factoryBean = new DummyRepositoryFactoryBean(SampleRepository.class);
|
||||
factoryBean.setBeanClassLoader(classLoader);
|
||||
factoryBean.setLazyInit(true);
|
||||
factoryBean.setRepositoryInterface(SampleRepository.class);
|
||||
factoryBean.afterPropertiesSet();
|
||||
|
||||
Object factory = ReflectionTestUtils.getField(factoryBean, "factory");
|
||||
@@ -58,14 +57,13 @@ public class RepositoryFactoryBeanSupportUnitTests {
|
||||
* @see DATACMNS-432
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void initializationFailsWithMissingRepositoryInterface() {
|
||||
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
exception.expectMessage("Repository interface");
|
||||
|
||||
RepositoryFactoryBeanSupport factoryBean = new DummyRepositoryFactoryBean();
|
||||
factoryBean.afterPropertiesSet();
|
||||
new DummyRepositoryFactoryBean(null);
|
||||
}
|
||||
|
||||
interface SampleRepository extends Repository<Object, Long> {}
|
||||
|
||||
@@ -88,7 +88,7 @@ public class TransactionRepositoryFactoryBeanSupportUnitTests {
|
||||
private final CrudRepository<Object, Long> repository = mock(CrudRepository.class);
|
||||
|
||||
public SampleTransactionalRepositoryFactoryBean() {
|
||||
setRepositoryInterface((Class) CrudRepository.class);
|
||||
super((Class) CrudRepository.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -24,18 +24,15 @@ public class SampleConfiguration {
|
||||
@Bean
|
||||
public RepositoryFactoryBeanSupport<Repository<User, Long>, User, Long> userRepositoryFactory() {
|
||||
|
||||
DummyRepositoryFactoryBean<Repository<User, Long>, User, Long> factory = new DummyRepositoryFactoryBean<Repository<User, Long>, User, Long>();
|
||||
factory.setRepositoryInterface(UserRepository.class);
|
||||
|
||||
return factory;
|
||||
return new DummyRepositoryFactoryBean<Repository<User, Long>, User, Long>(UserRepository.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RepositoryFactoryBeanSupport<Repository<Product, Long>, Product, Long> productRepositoryFactory(
|
||||
ProductRepository productRepository) {
|
||||
|
||||
DummyRepositoryFactoryBean<Repository<Product, Long>, Product, Long> factory = new DummyRepositoryFactoryBean<Repository<Product, Long>, Product, Long>();
|
||||
factory.setRepositoryInterface(ProductRepository.class);
|
||||
DummyRepositoryFactoryBean<Repository<Product, Long>, Product, Long> factory = new DummyRepositoryFactoryBean<Repository<Product, Long>, Product, Long>(
|
||||
ProductRepository.class);
|
||||
factory.setCustomImplementation(productRepository);
|
||||
|
||||
return factory;
|
||||
|
||||
@@ -214,7 +214,7 @@ public class DomainClassConverterUnitTests {
|
||||
private ApplicationContext initContextWithRepo() {
|
||||
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class);
|
||||
builder.addPropertyValue("repositoryInterface", UserRepository.class);
|
||||
builder.addConstructorArgValue(UserRepository.class);
|
||||
|
||||
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
|
||||
factory.registerBeanDefinition("provider", builder.getBeanDefinition());
|
||||
|
||||
@@ -73,7 +73,7 @@ public class RepositoriesUnitTests {
|
||||
private AbstractBeanDefinition getRepositoryBeanDefinition(Class<?> repositoryInterface) {
|
||||
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class);
|
||||
builder.addPropertyValue("repositoryInterface", repositoryInterface);
|
||||
builder.addConstructorArgValue(repositoryInterface);
|
||||
|
||||
return builder.getBeanDefinition();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user