Skip to content

Commit df460f4

Browse files
committed
added "packagesToScan" feature to LocalContainerEntityManagerFactoryBean (avoiding persistence.xml)
1 parent e5d5515 commit df460f4

File tree

2 files changed

+128
-8
lines changed

2 files changed

+128
-8
lines changed

org.springframework.orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,24 @@ public void setPersistenceUnitManager(PersistenceUnitManager persistenceUnitMana
120120
* @see #setPersistenceUnitManager
121121
*/
122122
public void setPersistenceXmlLocation(String persistenceXmlLocation) {
123-
this.internalPersistenceUnitManager.setPersistenceXmlLocations(new String[] {persistenceXmlLocation});
123+
this.internalPersistenceUnitManager.setPersistenceXmlLocation(persistenceXmlLocation);
124+
}
125+
126+
/**
127+
* Set whether to use Spring-based scanning for entity classes in the classpath
128+
* instead of using JPA's standard scanning of jar files with <code>persistence.xml</code>
129+
* markers in them. In case of Spring-based scanning, no <code>persistence.xml</code>
130+
* is necessary; all you need to do is to specify base packages to search here.
131+
* <p>Default is none. Specify packages to search for autodetection of your entity
132+
* classes in the classpath. This is analogous to Spring's component-scan feature
133+
* ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}).
134+
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
135+
* @param packagesToScan one or more base packages to search, analogous to
136+
* Spring's component-scan configuration for regular Spring components
137+
* @see #setPersistenceUnitManager
138+
*/
139+
public void setPackagesToScan(String[] packagesToScan) {
140+
this.internalPersistenceUnitManager.setPackagesToScan(packagesToScan);
124141
}
125142

126143
/**

org.springframework.orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java

Lines changed: 110 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,10 +21,16 @@
2121
import java.lang.reflect.Method;
2222
import java.lang.reflect.Proxy;
2323
import java.net.URL;
24+
import java.util.Arrays;
2425
import java.util.HashMap;
2526
import java.util.HashSet;
27+
import java.util.LinkedList;
28+
import java.util.List;
2629
import java.util.Map;
2730
import java.util.Set;
31+
import javax.persistence.Embeddable;
32+
import javax.persistence.Entity;
33+
import javax.persistence.MappedSuperclass;
2834
import javax.persistence.PersistenceException;
2935
import javax.persistence.spi.PersistenceUnitInfo;
3036
import javax.sql.DataSource;
@@ -37,6 +43,11 @@
3743
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
3844
import org.springframework.core.io.support.ResourcePatternResolver;
3945
import org.springframework.core.io.support.ResourcePatternUtils;
46+
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
47+
import org.springframework.core.type.classreading.MetadataReader;
48+
import org.springframework.core.type.classreading.MetadataReaderFactory;
49+
import org.springframework.core.type.filter.AnnotationTypeFilter;
50+
import org.springframework.core.type.filter.TypeFilter;
4051
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
4152
import org.springframework.instrument.classloading.LoadTimeWeaver;
4253
import org.springframework.jdbc.datasource.lookup.DataSourceLookup;
@@ -80,13 +91,26 @@ public class DefaultPersistenceUnitManager
8091
*/
8192
public final static String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION = "classpath:";
8293

94+
public final static String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME = "default";
95+
96+
private static final String RESOURCE_PATTERN = "/**/*.class";
97+
8398

8499
private static final boolean jpa2ApiPresent = ClassUtils.hasMethod(PersistenceUnitInfo.class, "getSharedCacheMode");
85100

101+
private static final TypeFilter[] entityTypeFilters = new TypeFilter[] {
102+
new AnnotationTypeFilter(Entity.class, false),
103+
new AnnotationTypeFilter(Embeddable.class, false),
104+
new AnnotationTypeFilter(MappedSuperclass.class, false)};
105+
86106
private String[] persistenceXmlLocations = new String[] {DEFAULT_PERSISTENCE_XML_LOCATION};
87107

88108
private String defaultPersistenceUnitRootLocation = ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION;
89109

110+
private String defaultPersistenceUnitName = ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME;
111+
112+
private String[] packagesToScan;
113+
90114
private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
91115

92116
private DataSource defaultDataSource;
@@ -133,6 +157,30 @@ public void setDefaultPersistenceUnitRootLocation(String defaultPersistenceUnitR
133157
this.defaultPersistenceUnitRootLocation = defaultPersistenceUnitRootLocation;
134158
}
135159

160+
/**
161+
* Specify the name of the default persistence unit, if any. Default is "default".
162+
* <p>Primarily applied to a scanned persistence unit without <code>persistence.xml</code>.
163+
* Also applicable to selecting a default unit from several persistence units available.
164+
* @see #setPackagesToScan
165+
* @see #obtainDefaultPersistenceUnitInfo
166+
*/
167+
public void setDefaultPersistenceUnitName(String defaultPersistenceUnitName) {
168+
this.defaultPersistenceUnitName = defaultPersistenceUnitName;
169+
}
170+
171+
/**
172+
* Set whether to use Spring-based scanning for entity classes in the classpath
173+
* instead of using JPA's standard scanning of jar files with <code>persistence.xml</code>
174+
* markers in them. In case of Spring-based scanning, no <code>persistence.xml</code>
175+
* is necessary; all you need to do is to specify base packages to search here.
176+
* <p>Default is none. Specify packages to search for autodetection of your entity
177+
* classes in the classpath. This is analogous to Spring's component-scan feature
178+
* ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}).
179+
*/
180+
public void setPackagesToScan(String[] packagesToScan) {
181+
this.packagesToScan = packagesToScan;
182+
}
183+
136184
/**
137185
* Specify the JDBC DataSources that the JPA persistence provider is supposed
138186
* to use for accessing the database, resolving data source names in
@@ -273,7 +321,7 @@ public void afterPropertiesSet() {
273321
public void preparePersistenceUnitInfos() {
274322
this.persistenceUnitInfoNames.clear();
275323
this.persistenceUnitInfos.clear();
276-
SpringPersistenceUnitInfo[] puis = readPersistenceUnitInfos();
324+
List<SpringPersistenceUnitInfo> puis = readPersistenceUnitInfos();
277325
for (SpringPersistenceUnitInfo pui : puis) {
278326
if (pui.getPersistenceUnitRootUrl() == null) {
279327
pui.setPersistenceUnitRootUrl(determineDefaultPersistenceUnitRootUrl());
@@ -289,7 +337,13 @@ public void preparePersistenceUnitInfos() {
289337
}
290338
postProcessPersistenceUnitInfo(pui);
291339
String name = pui.getPersistenceUnitName();
292-
this.persistenceUnitInfoNames.add(name);
340+
if (!this.persistenceUnitInfoNames.add(name)) {
341+
StringBuilder msg = new StringBuilder();
342+
msg.append("Conflicting persistence unit definitions for name '").append(name).append("': ");
343+
msg.append(pui.getPersistenceUnitRootUrl()).append(", ");
344+
msg.append(this.persistenceUnitInfos.get(name).getPersistenceUnitRootUrl());
345+
throw new IllegalStateException(msg.toString());
346+
}
293347
PersistenceUnitInfo puiToStore = pui;
294348
if (jpa2ApiPresent) {
295349
puiToStore = (PersistenceUnitInfo) Proxy.newProxyInstance(SmartPersistenceUnitInfo.class.getClassLoader(),
@@ -303,9 +357,58 @@ public void preparePersistenceUnitInfos() {
303357
* Read all persistence unit infos from <code>persistence.xml</code>,
304358
* as defined in the JPA specification.
305359
*/
306-
private SpringPersistenceUnitInfo[] readPersistenceUnitInfos() {
360+
private List<SpringPersistenceUnitInfo> readPersistenceUnitInfos() {
307361
PersistenceUnitReader reader = new PersistenceUnitReader(this.resourcePatternResolver, this.dataSourceLookup);
308-
return reader.readPersistenceUnitInfos(this.persistenceXmlLocations);
362+
List<SpringPersistenceUnitInfo> infos = new LinkedList<SpringPersistenceUnitInfo>();
363+
infos.addAll(Arrays.asList(reader.readPersistenceUnitInfos(this.persistenceXmlLocations)));
364+
if (this.packagesToScan != null) {
365+
infos.add(scanPackages());
366+
}
367+
return infos;
368+
}
369+
370+
/**
371+
* Perform Spring-based scanning for entity classes.
372+
* @see #setPackagesToScan
373+
*/
374+
private SpringPersistenceUnitInfo scanPackages() {
375+
SpringPersistenceUnitInfo scannedUnit = new SpringPersistenceUnitInfo();
376+
try {
377+
scannedUnit.setPersistenceUnitName(this.defaultPersistenceUnitName);
378+
scannedUnit.excludeUnlistedClasses();
379+
for (String pkg : this.packagesToScan) {
380+
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
381+
ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN;
382+
Resource[] resources = this.resourcePatternResolver.getResources(pattern);
383+
MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
384+
for (Resource resource : resources) {
385+
if (resource.isReadable()) {
386+
MetadataReader reader = readerFactory.getMetadataReader(resource);
387+
String className = reader.getClassMetadata().getClassName();
388+
if (matchesFilter(reader, readerFactory)) {
389+
scannedUnit.addManagedClassName(className);
390+
}
391+
}
392+
}
393+
}
394+
return scannedUnit;
395+
}
396+
catch (IOException ex) {
397+
throw new PersistenceException("Failed to scan classpath for unlisted classes", ex);
398+
}
399+
}
400+
401+
/**
402+
* Check whether any of the configured entity type filters matches
403+
* the current class descriptor contained in the metadata reader.
404+
*/
405+
private boolean matchesFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException {
406+
for (TypeFilter filter : entityTypeFilters) {
407+
if (filter.match(reader, readerFactory)) {
408+
return true;
409+
}
410+
}
411+
return false;
309412
}
310413

311414
/**
@@ -327,6 +430,7 @@ private URL determineDefaultPersistenceUnitRootUrl() {
327430
}
328431
}
329432

433+
330434
/**
331435
* Return the specified PersistenceUnitInfo from this manager's cache
332436
* of processed persistence units, keeping it in the cache (i.e. not
@@ -378,8 +482,7 @@ public PersistenceUnitInfo obtainDefaultPersistenceUnitInfo() {
378482
ObjectUtils.nullSafeToString(this.persistenceXmlLocations) + " already obtained");
379483
}
380484
if (this.persistenceUnitInfos.size() > 1) {
381-
throw new IllegalStateException("No single default persistence unit defined in " +
382-
ObjectUtils.nullSafeToString(this.persistenceXmlLocations));
485+
return obtainPersistenceUnitInfo(this.defaultPersistenceUnitName);
383486
}
384487
PersistenceUnitInfo pui = this.persistenceUnitInfos.values().iterator().next();
385488
this.persistenceUnitInfos.clear();

0 commit comments

Comments
 (0)