📄 defaultpersistenceunitmanager.java
字号:
/*
* Copyright 2002-2007 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.orm.jpa.persistenceunit;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.persistence.PersistenceException;
import javax.persistence.spi.PersistenceUnitInfo;
import javax.sql.DataSource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ResourceLoaderAware;
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.instrument.classloading.LoadTimeWeaver;
import org.springframework.jdbc.datasource.lookup.DataSourceLookup;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.jdbc.datasource.lookup.MapDataSourceLookup;
import org.springframework.util.ObjectUtils;
/**
* Default implementation of the {@link PersistenceUnitManager} interface.
* Used as internal default by
* {@link org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean}.
*
* <p>Supports standard JPA scanning for <code>persistence.xml</code> files,
* with configurable file locations, JDBC DataSource lookup and load-time weaving.
*
* <p>The default XML file location is <code>classpath:META-INF/persistence.xml</code>,
* scanning for all matching files in the class path (as defined in the JPA specification).
* DataSource names are by default interpreted as JNDI names, and no load time weaving
* is available (which requires weaving to be turned off in the persistence provider).
*
* @author Juergen Hoeller
* @since 2.0
* @see #setPersistenceXmlLocations
* @see #setDataSourceLookup
* @see #setLoadTimeWeaver
* @see org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean#setPersistenceUnitManager
*/
public class DefaultPersistenceUnitManager implements PersistenceUnitManager, ResourceLoaderAware, InitializingBean {
/**
* Default location of the <code>persistence.xml</code> file:
* "classpath*:META-INF/persistence.xml".
*/
public final static String DEFAULT_PERSISTENCE_XML_LOCATION = "classpath*:META-INF/persistence.xml";
/**
* Default location for the persistence unit root URL:
* "classpath:", indicating the root of the class path.
*/
public final static String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION = "classpath:";
/** Location of persistence.xml file(s) */
private String[] persistenceXmlLocations = new String[] {DEFAULT_PERSISTENCE_XML_LOCATION};
private String defaultPersistenceUnitRootLocation = ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION;
private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
private DataSource defaultDataSource;
private LoadTimeWeaver loadTimeWeaver;
private PersistenceUnitPostProcessor[] persistenceUnitPostProcessors;
private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
private final Set<String> persistenceUnitInfoNames = new HashSet<String>();
private final Map<String, MutablePersistenceUnitInfo> persistenceUnitInfos =
new HashMap<String, MutablePersistenceUnitInfo>();
/**
* Set the locations of the <code>persistence.xml</code> files to load.
* These can be specified as Spring resource locations and/or location patterns.
* <p>Default is "classpath*:META-INF/persistence.xml".
* @param persistenceXmlLocations an array of Spring resource Strings
* identifying the location of the <code>persistence.xml</code> files to read
*/
public void setPersistenceXmlLocations(String[] persistenceXmlLocations) {
this.persistenceXmlLocations = persistenceXmlLocations;
}
/**
* Set the default persistence unit root location, to be applied
* if no unit-specific persistence unit root could be determined.
* <p>Default is "classpath:", that is, the root of the current class path
* (nearest root directory). To be overridden if unit-specific resolution
* does not work and the class path root is not appropriate either.
*/
public void setDefaultPersistenceUnitRootLocation(String defaultPersistenceUnitRootLocation) {
this.defaultPersistenceUnitRootLocation = defaultPersistenceUnitRootLocation;
}
/**
* Specify the JDBC DataSources that the JPA persistence provider is supposed
* to use for accessing the database, resolving data source names in
* <code>persistence.xml</code> against Spring-managed DataSources.
* <p>The specified Map needs to define data source names for specific DataSource
* objects, matching the data source names used in <code>persistence.xml</code>.
* If not specified, data source names will be resolved as JNDI names instead
* (as defined by standard JPA).
* @see org.springframework.jdbc.datasource.lookup.MapDataSourceLookup
*/
public void setDataSources(Map<String, DataSource> dataSources) {
this.dataSourceLookup = new MapDataSourceLookup(dataSources);
}
/**
* Specify the JDBC DataSourceLookup that provides DataSources for the
* persistence provider, resolving data source names in <code>persistence.xml</code>
* against Spring-managed DataSource instances.
* <p>Default is JndiDataSourceLookup, which resolves DataSource names as
* JNDI names (as defined by standard JPA). Specify a BeanFactoryDataSourceLookup
* instance if you want DataSource names to be resolved against Spring bean names.
* <p>Alternatively, consider passing in a map from names to DataSource instances
* via the "dataSources" property. If the <code>persistence.xml</code> file
* does not define DataSource names at all, specify a default DataSource
* via the "defaultDataSource" property.
* @see org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup
* @see org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup
* @see #setDataSources
* @see #setDefaultDataSource
*/
public void setDataSourceLookup(DataSourceLookup dataSourceLookup) {
this.dataSourceLookup = (dataSourceLookup != null ? dataSourceLookup : new JndiDataSourceLookup());
}
/**
* Return the JDBC DataSourceLookup that provides DataSources for the
* persistence provider, resolving data source names in <code>persistence.xml</code>
* against Spring-managed DataSource instances.
*/
public DataSourceLookup getDataSourceLookup() {
return this.dataSourceLookup;
}
/**
* Specify the JDBC DataSource that the JPA persistence provider is supposed
* to use for accessing the database if none has been specified in
* <code>persistence.xml</code>.
* <p>In JPA speak, a DataSource passed in here will be uses as "nonJtaDataSource"
* on the PersistenceUnitInfo passed to the PersistenceProvider, provided that
* none has been registered before.
* @see javax.persistence.spi.PersistenceUnitInfo#getNonJtaDataSource()
*/
public void setDefaultDataSource(DataSource defaultDataSource) {
this.defaultDataSource = defaultDataSource;
}
/**
* Return the JDBC DataSource that the JPA persistence provider is supposed
* to use for accessing the database if none has been specified in
* <code>persistence.xml</code>.
*/
public DataSource getDefaultDataSource() {
return this.defaultDataSource;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -