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:
@@ -82,13 +82,13 @@ class RepositoryBeanDefinitionBuilder {
|
||||
Assert.notNull(resourceLoader, "ResourceLoader must not be null!");
|
||||
|
||||
String factoryBeanName = configuration.getRepositoryFactoryBeanName();
|
||||
factoryBeanName = StringUtils.hasText(factoryBeanName) ? factoryBeanName : extension
|
||||
.getRepositoryFactoryClassName();
|
||||
factoryBeanName = StringUtils.hasText(factoryBeanName) ? factoryBeanName
|
||||
: extension.getRepositoryFactoryClassName();
|
||||
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(factoryBeanName);
|
||||
|
||||
builder.getRawBeanDefinition().setSource(configuration.getSource());
|
||||
builder.addPropertyValue("repositoryInterface", configuration.getRepositoryInterface());
|
||||
builder.addConstructorArgValue(configuration.getRepositoryInterface());
|
||||
builder.addPropertyValue("queryLookupStrategyKey", configuration.getQueryLookupStrategyKey());
|
||||
builder.addPropertyValue("lazyInit", configuration.isLazyInit());
|
||||
builder.addPropertyValue("repositoryBaseClass", configuration.getRepositoryBaseClassName());
|
||||
@@ -127,8 +127,8 @@ class RepositoryBeanDefinitionBuilder {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
AbstractBeanDefinition beanDefinition = implementationDetector.detectCustomImplementation(
|
||||
configuration.getImplementationClassName(), configuration.getBasePackages());
|
||||
AbstractBeanDefinition beanDefinition = implementationDetector
|
||||
.detectCustomImplementation(configuration.getImplementationClassName(), configuration.getBasePackages());
|
||||
|
||||
if (null == beanDefinition) {
|
||||
return null;
|
||||
|
||||
@@ -65,7 +65,7 @@ public class RepositoryBeanNameGenerator implements BeanNameGenerator, BeanClass
|
||||
*/
|
||||
private Class<?> getRepositoryInterfaceFrom(BeanDefinition beanDefinition) {
|
||||
|
||||
Object value = beanDefinition.getPropertyValues().getPropertyValue("repositoryInterface").getValue();
|
||||
Object value = beanDefinition.getConstructorArgumentValues().getArgumentValue(0, Class.class).getValue();
|
||||
|
||||
if (value instanceof Class<?>) {
|
||||
return (Class<?>) value;
|
||||
|
||||
@@ -33,7 +33,6 @@ import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.AbstractRepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -51,8 +50,6 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit
|
||||
private static final String CLASS_LOADING_ERROR = "%s - Could not load type %s using class loader %s.";
|
||||
private static final String MULTI_STORE_DROPPED = "Spring Data {} - Could not safely identify store assignment for repository candidate {}.";
|
||||
|
||||
private static final String FACTORY_BEAN_TYPE_PREDICTING_POST_PROCESSOR = "org.springframework.data.repository.core.support.FactoryBeanTypePredictingBeanPostProcessor";
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#getModuleName()
|
||||
@@ -125,18 +122,8 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#registerBeansForRoot(org.springframework.beans.factory.support.BeanDefinitionRegistry, org.springframework.data.repository.config.RepositoryConfigurationSource)
|
||||
*/
|
||||
public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConfigurationSource configurationSource) {
|
||||
|
||||
String typeName = RepositoryFactoryBeanSupport.class.getName();
|
||||
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder
|
||||
.rootBeanDefinition(FACTORY_BEAN_TYPE_PREDICTING_POST_PROCESSOR);
|
||||
builder.addConstructorArgValue(typeName);
|
||||
builder.addConstructorArgValue("repositoryInterface");
|
||||
|
||||
registerIfNotAlreadyRegistered(builder.getBeanDefinition(), registry, typeName.concat("_Predictor"),
|
||||
configurationSource.getSource());
|
||||
}
|
||||
public void registerBeansForRoot(BeanDefinitionRegistry registry,
|
||||
RepositoryConfigurationSource configurationSource) {}
|
||||
|
||||
/**
|
||||
* Returns the prefix of the module to be used to create the default location for Spring Data named queries.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,6 @@ import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Required;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
@@ -53,10 +52,10 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>,
|
||||
implements InitializingBean, RepositoryFactoryInformation<S, ID>, FactoryBean<T>, BeanClassLoaderAware,
|
||||
BeanFactoryAware, ApplicationEventPublisherAware {
|
||||
|
||||
private RepositoryFactorySupport factory;
|
||||
private final Class<? extends T> repositoryInterface;
|
||||
|
||||
private RepositoryFactorySupport factory;
|
||||
private Key queryLookupStrategyKey;
|
||||
private Class<? extends T> repositoryInterface;
|
||||
private Class<?> repositoryBaseClass;
|
||||
private Object customImplementation;
|
||||
private NamedQueries namedQueries;
|
||||
@@ -72,14 +71,13 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>,
|
||||
private RepositoryMetadata repositoryMetadata;
|
||||
|
||||
/**
|
||||
* Setter to inject the repository interface to implement.
|
||||
* Creates a new {@link RepositoryFactoryBeanSupport} for the given repository interface.
|
||||
*
|
||||
* @param repositoryInterface the repository interface to set
|
||||
* @param repositoryInterface must not be {@literal null}.
|
||||
*/
|
||||
@Required
|
||||
public void setRepositoryInterface(Class<? extends T> repositoryInterface) {
|
||||
protected RepositoryFactoryBeanSupport(Class<? extends T> repositoryInterface) {
|
||||
|
||||
Assert.notNull(repositoryInterface);
|
||||
Assert.notNull(repositoryInterface, "Repository interface must not be null!");
|
||||
this.repositoryInterface = repositoryInterface;
|
||||
}
|
||||
|
||||
@@ -229,9 +227,8 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>,
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Class<? extends T> getObjectType() {
|
||||
return (Class<? extends T>) (null == repositoryInterface ? Repository.class : repositoryInterface);
|
||||
return repositoryInterface;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -248,8 +245,6 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>,
|
||||
*/
|
||||
public void afterPropertiesSet() {
|
||||
|
||||
Assert.notNull(repositoryInterface, "Repository interface must not be null on initialization!");
|
||||
|
||||
this.factory = createRepositoryFactory();
|
||||
this.factory.setQueryLookupStrategyKey(queryLookupStrategyKey);
|
||||
this.factory.setNamedQueries(namedQueries);
|
||||
|
||||
@@ -40,6 +40,15 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi
|
||||
private RepositoryProxyPostProcessor exceptionPostProcessor;
|
||||
private boolean enableDefaultTransactions = true;
|
||||
|
||||
/**
|
||||
* Creates a new {@link TransactionalRepositoryFactoryBeanSupport} for the given repository interface.
|
||||
*
|
||||
* @param repositoryInterface must not be {@literal null}.
|
||||
*/
|
||||
protected TransactionalRepositoryFactoryBeanSupport(Class<? extends T> repositoryInterface) {
|
||||
super(repositoryInterface);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter to configure which transaction manager to be used. We have to use the bean name explicitly as otherwise the
|
||||
* qualifier of the {@link org.springframework.transaction.annotation.Transactional} annotation is used. By explicitly
|
||||
|
||||
Reference in New Issue
Block a user