Introduce configuration of class loader in SimpleTypeInformationMapper.
We now support configuration of the class loader in SimpleTypeInformationMapper to use a configured class loader instead of falling always back to the default/contextual class loader. In arrangements where the contextual class loader isn't able to provide access to the desired classes (e.g. parallel Streams, general Fork/Join Thread usage) the contextual class loader may not have access to the entity types. By implementing BeanClassLoaderAware, we can now propagate a configured class loader. Closes #2508
This commit is contained in:
@@ -23,6 +23,7 @@ import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.data.mapping.Alias;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
@@ -39,7 +40,7 @@ import org.springframework.util.Assert;
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class DefaultTypeMapper<S> implements TypeMapper<S> {
|
||||
public class DefaultTypeMapper<S> implements TypeMapper<S>, BeanClassLoaderAware {
|
||||
|
||||
private final TypeAliasAccessor<S> accessor;
|
||||
private final List<? extends TypeInformationMapper> mappers;
|
||||
@@ -215,6 +216,19 @@ public class DefaultTypeMapper<S> implements TypeMapper<S> {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang.ClassLoader)
|
||||
*/
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
for (TypeInformationMapper mapper : mappers) {
|
||||
if (mapper instanceof BeanClassLoaderAware) {
|
||||
((BeanClassLoaderAware) mapper).setBeanClassLoader(classLoader);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the alias to be used for the given {@link TypeInformation}.
|
||||
*
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.data.mapping.Alias;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
@@ -33,10 +34,12 @@ import org.springframework.util.ClassUtils;
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class SimpleTypeInformationMapper implements TypeInformationMapper {
|
||||
public class SimpleTypeInformationMapper implements TypeInformationMapper, BeanClassLoaderAware {
|
||||
|
||||
private final Map<String, Optional<ClassTypeInformation<?>>> cache = new ConcurrentHashMap<>();
|
||||
|
||||
private @Nullable ClassLoader classLoader;
|
||||
|
||||
/**
|
||||
* Returns the {@link TypeInformation} that shall be used when the given {@link String} value is found as type hint.
|
||||
* The implementation will simply interpret the given value as fully-qualified class name and try to load the class.
|
||||
@@ -53,7 +56,7 @@ public class SimpleTypeInformationMapper implements TypeInformationMapper {
|
||||
String stringAlias = alias.mapTyped(String.class);
|
||||
|
||||
if (stringAlias != null) {
|
||||
return cache.computeIfAbsent(stringAlias, SimpleTypeInformationMapper::loadClass).orElse(null);
|
||||
return cache.computeIfAbsent(stringAlias, this::loadClass).orElse(null);
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -70,10 +73,19 @@ public class SimpleTypeInformationMapper implements TypeInformationMapper {
|
||||
return Alias.of(type.getType().getName());
|
||||
}
|
||||
|
||||
private static Optional<ClassTypeInformation<?>> loadClass(String typeName) {
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang.ClassLoader)
|
||||
*/
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
this.classLoader = classLoader;
|
||||
}
|
||||
|
||||
private Optional<ClassTypeInformation<?>> loadClass(String typeName) {
|
||||
|
||||
try {
|
||||
return Optional.of(ClassTypeInformation.from(ClassUtils.forName(typeName, null)));
|
||||
return Optional.of(ClassTypeInformation.from(ClassUtils.forName(typeName, this.classLoader)));
|
||||
} catch (ClassNotFoundException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user