DATAJPA-631 - ResourceLoader improvements in ClasspathScanningPersistenceUnitPostProcessor.
A custom ResourceLoader configured on ClasspathScanningPersistenceUnitPostProcessor is now propagated to the ClassPathScanningCandidateComponentProvider used for looking up entity classes. We also forward the configured environment so that profile annotations are evaluated as well. Related issue: DATACMNS-591.
This commit is contained in:
@@ -27,12 +27,17 @@ import javax.persistence.MappedSuperclass;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.ResourceLoaderAware;
|
||||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
import org.springframework.core.io.support.ResourcePatternUtils;
|
||||
import org.springframework.core.type.filter.AnnotationTypeFilter;
|
||||
import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo;
|
||||
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor;
|
||||
@@ -47,13 +52,16 @@ import org.springframework.util.StringUtils;
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class ClasspathScanningPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor, ResourceLoaderAware {
|
||||
public class ClasspathScanningPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor,
|
||||
ResourceLoaderAware, EnvironmentAware {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ClasspathScanningPersistenceUnitPostProcessor.class);
|
||||
|
||||
private final String basePackage;
|
||||
|
||||
private ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader());
|
||||
private ResourcePatternResolver mappingFileResolver = new PathMatchingResourcePatternResolver();
|
||||
private Environment environment = new StandardEnvironment();
|
||||
private ResourceLoader resourceLoader = new DefaultResourceLoader();
|
||||
private String mappingFileNamePattern;
|
||||
|
||||
/**
|
||||
@@ -62,7 +70,8 @@ public class ClasspathScanningPersistenceUnitPostProcessor implements Persistenc
|
||||
* @param basePackage must not be {@literal null} or empty.
|
||||
*/
|
||||
public ClasspathScanningPersistenceUnitPostProcessor(String basePackage) {
|
||||
Assert.hasText(basePackage);
|
||||
|
||||
Assert.hasText(basePackage, "Base package must not be null!");
|
||||
this.basePackage = basePackage;
|
||||
}
|
||||
|
||||
@@ -73,7 +82,8 @@ public class ClasspathScanningPersistenceUnitPostProcessor implements Persistenc
|
||||
* @param mappingFilePattern must not be {@literal null} or empty.
|
||||
*/
|
||||
public void setMappingFileNamePattern(String mappingFilePattern) {
|
||||
Assert.hasText(mappingFilePattern);
|
||||
|
||||
Assert.hasText(mappingFilePattern, "Mapping file pattern must not be null or empty!");
|
||||
this.mappingFileNamePattern = mappingFilePattern;
|
||||
}
|
||||
|
||||
@@ -82,8 +92,21 @@ public class ClasspathScanningPersistenceUnitPostProcessor implements Persistenc
|
||||
* @see org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader)
|
||||
*/
|
||||
public void setResourceLoader(ResourceLoader resourceLoader) {
|
||||
Assert.notNull(resourceLoader);
|
||||
this.resolver = new PathMatchingResourcePatternResolver(resourceLoader);
|
||||
|
||||
Assert.notNull(resourceLoader, "ResourceLoader must not be null!");
|
||||
this.mappingFileResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.context.EnvironmentAware#setEnvironment(org.springframework.core.env.Environment)
|
||||
*/
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
|
||||
Assert.notNull(environment, "Environment must not be null!");
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -93,6 +116,9 @@ public class ClasspathScanningPersistenceUnitPostProcessor implements Persistenc
|
||||
public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
|
||||
|
||||
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
|
||||
|
||||
provider.setEnvironment(environment);
|
||||
provider.setResourceLoader(resourceLoader);
|
||||
provider.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
|
||||
provider.addIncludeFilter(new AnnotationTypeFilter(MappedSuperclass.class));
|
||||
|
||||
@@ -106,7 +132,6 @@ public class ClasspathScanningPersistenceUnitPostProcessor implements Persistenc
|
||||
LOG.debug("Registering classpath-scanned entity mapping file in persistence unit info!", location);
|
||||
pui.addMappingFileName(location);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,7 +144,7 @@ public class ClasspathScanningPersistenceUnitPostProcessor implements Persistenc
|
||||
*/
|
||||
private Set<String> scanForMappingFileLocations() {
|
||||
|
||||
if (resolver == null || !StringUtils.hasText(mappingFileNamePattern)) {
|
||||
if (!StringUtils.hasText(mappingFileNamePattern)) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
@@ -137,7 +162,7 @@ public class ClasspathScanningPersistenceUnitPostProcessor implements Persistenc
|
||||
Resource[] scannedResources;
|
||||
|
||||
try {
|
||||
scannedResources = resolver.getResources(path);
|
||||
scannedResources = mappingFileResolver.getResources(path);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(String.format("Cannot load mapping files from path %s!", path), e);
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ public class ClasspathScanningPersistenceUnitPostProcessorUnitTests {
|
||||
|
||||
ClasspathScanningPersistenceUnitPostProcessor processor = new ClasspathScanningPersistenceUnitPostProcessor(
|
||||
basePackage);
|
||||
ReflectionTestUtils.setField(processor, "resolver", resolver);
|
||||
ReflectionTestUtils.setField(processor, "mappingFileResolver", resolver);
|
||||
processor.setMappingFileNamePattern("**/*orm.xml");
|
||||
processor.postProcessPersistenceUnitInfo(pui);
|
||||
|
||||
@@ -162,7 +162,5 @@ public class ClasspathScanningPersistenceUnitPostProcessorUnitTests {
|
||||
}
|
||||
|
||||
@Entity
|
||||
public static class SampleEntity {
|
||||
|
||||
}
|
||||
public static class SampleEntity {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user