Browse Source
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.pull/190/head
17 changed files with 42 additions and 476 deletions
@ -1,182 +0,0 @@
@@ -1,182 +0,0 @@
|
||||
/* |
||||
* Copyright 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 java.util.Arrays; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.PropertyValue; |
||||
import org.springframework.beans.factory.BeanFactory; |
||||
import org.springframework.beans.factory.BeanFactoryAware; |
||||
import org.springframework.beans.factory.FactoryBean; |
||||
import org.springframework.beans.factory.config.BeanDefinition; |
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
||||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; |
||||
import org.springframework.beans.factory.config.TypedStringValue; |
||||
import org.springframework.core.Ordered; |
||||
import org.springframework.core.PriorityOrdered; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.ClassUtils; |
||||
|
||||
/** |
||||
* {@link InstantiationAwareBeanPostProcessorAdapter} to predict the bean type for {@link FactoryBean} implementations |
||||
* by interpreting a configured property of the {@link BeanDefinition} as type to be created eventually. |
||||
* |
||||
* @author Oliver Gierke |
||||
* @since 1.12 |
||||
* @soundtrack Ron Spielmann - Lock Me Up (Electric Tales) |
||||
*/ |
||||
public class FactoryBeanTypePredictingBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter |
||||
implements BeanFactoryAware, PriorityOrdered { |
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FactoryBeanTypePredictingBeanPostProcessor.class); |
||||
|
||||
private final Map<String, Class<?>> cache = new ConcurrentHashMap<String, Class<?>>(); |
||||
private final Class<?> factoryBeanType; |
||||
private final List<String> properties; |
||||
private ConfigurableListableBeanFactory context; |
||||
|
||||
/** |
||||
* Creates a new {@link FactoryBeanTypePredictingBeanPostProcessor} predicting the type created by the |
||||
* {@link FactoryBean} of the given type by inspecting the {@link BeanDefinition} and considering the value for the |
||||
* given property as type to be created eventually. |
||||
* |
||||
* @param factoryBeanType must not be {@literal null}. |
||||
* @param properties must not be {@literal null} or empty. |
||||
*/ |
||||
public FactoryBeanTypePredictingBeanPostProcessor(Class<?> factoryBeanType, String... properties) { |
||||
|
||||
Assert.notNull(factoryBeanType, "FactoryBean type must not be null!"); |
||||
Assert.isTrue(FactoryBean.class.isAssignableFrom(factoryBeanType), "Given type is not a FactoryBean type!"); |
||||
Assert.notEmpty(properties, "Properties must not be empty!"); |
||||
|
||||
for (String property : properties) { |
||||
Assert.hasText(property, "Type property must not be null!"); |
||||
} |
||||
|
||||
this.factoryBeanType = factoryBeanType; |
||||
this.properties = Arrays.asList(properties); |
||||
} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory) |
||||
*/ |
||||
public void setBeanFactory(BeanFactory beanFactory) { |
||||
|
||||
if (beanFactory instanceof ConfigurableListableBeanFactory) { |
||||
this.context = (ConfigurableListableBeanFactory) beanFactory; |
||||
} |
||||
} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter#predictBeanType(java.lang.Class, java.lang.String) |
||||
*/ |
||||
@Override |
||||
public Class<?> predictBeanType(Class<?> beanClass, String beanName) { |
||||
|
||||
if (null == context || !factoryBeanType.isAssignableFrom(beanClass)) { |
||||
return null; |
||||
} |
||||
|
||||
Class<?> resolvedBeanClass = cache.get(beanName); |
||||
|
||||
if (resolvedBeanClass != null) { |
||||
return resolvedBeanClass == Void.class ? null : resolvedBeanClass; |
||||
} |
||||
|
||||
BeanDefinition definition = context.getBeanDefinition(beanName); |
||||
|
||||
try { |
||||
|
||||
for (String property : properties) { |
||||
|
||||
PropertyValue value = definition.getPropertyValues().getPropertyValue(property); |
||||
resolvedBeanClass = getClassForPropertyValue(value, beanName); |
||||
|
||||
if (Void.class.equals(resolvedBeanClass)) { |
||||
continue; |
||||
} |
||||
|
||||
return resolvedBeanClass; |
||||
} |
||||
|
||||
return null; |
||||
|
||||
} finally { |
||||
cache.put(beanName, resolvedBeanClass); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Returns the class which is configured in the given {@link PropertyValue}. In case it is not a |
||||
* {@link TypedStringValue} or the value contained cannot be interpreted as {@link Class} it will return {@link Void}. |
||||
* |
||||
* @param propertyValue can be {@literal null}. |
||||
* @param beanName must not be {@literal null}. |
||||
* @return |
||||
*/ |
||||
private Class<?> getClassForPropertyValue(PropertyValue propertyValue, String beanName) { |
||||
|
||||
if (propertyValue == null) { |
||||
return Void.class; |
||||
} |
||||
|
||||
Object value = propertyValue.getValue(); |
||||
String className = null; |
||||
|
||||
if (value instanceof TypedStringValue) { |
||||
className = ((TypedStringValue) value).getValue(); |
||||
} else if (value instanceof String) { |
||||
className = (String) value; |
||||
} else if (value instanceof Class<?>) { |
||||
return (Class<?>) value; |
||||
} else if (value instanceof String[]) { |
||||
|
||||
String[] values = (String[]) value; |
||||
|
||||
if (values.length == 0) { |
||||
return Void.class; |
||||
} else { |
||||
className = values[0]; |
||||
} |
||||
|
||||
} else { |
||||
return Void.class; |
||||
} |
||||
|
||||
try { |
||||
return ClassUtils.resolveClassName(className, context.getBeanClassLoader()); |
||||
} catch (IllegalArgumentException ex) { |
||||
LOGGER.warn( |
||||
String.format("Couldn't load class %s referenced as repository interface in bean %s!", className, beanName)); |
||||
return Void.class; |
||||
} |
||||
} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see org.springframework.core.Ordered#getOrder() |
||||
*/ |
||||
public int getOrder() { |
||||
return Ordered.LOWEST_PRECEDENCE - 1; |
||||
} |
||||
} |
||||
@ -1,78 +0,0 @@
@@ -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 @@
@@ -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> {} |
||||
} |
||||
Loading…
Reference in new issue