类 LocalPersistenceManagerFactoryBean
- java.lang.Object
- org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean
- 所有已实现的接口:
Aware
,BeanClassLoaderAware
,DisposableBean
,FactoryBean<javax.jdo.PersistenceManagerFactory>
,InitializingBean
,PersistenceExceptionTranslator
public class LocalPersistenceManagerFactoryBean extends Object implements FactoryBean<javax.jdo.PersistenceManagerFactory>, BeanClassLoaderAware, InitializingBean, DisposableBean, PersistenceExceptionTranslator
FactoryBean
that creates a JDOPersistenceManagerFactory
. This is the usual way to set up a shared JDO PersistenceManagerFactory in a Spring application context; the PersistenceManagerFactory can then be passed to JDO-based DAOs via dependency injection. Note that switching to a JNDI lookup or to a bean-style PersistenceManagerFactory instance is just a matter of configuration!NOTE: This class requires JDO 3.0 or higher, as of Spring 4.0. It will also expose the JPA
EntityManagerFactory
as long as the JDO provider creates ajavax.jdo.JDOEntityManagerFactory
reference underneath, which means that this class can be used as a replacement forLocalEntityManagerFactoryBean
in such a scenario.Configuration settings can either be read from a properties file, specified as "configLocation", or locally specified. Properties specified as "jdoProperties" here will override any settings in a file. You may alternatively specify a "persistenceManagerFactoryName", referring to a PMF definition in "META-INF/jdoconfig.xml" (see
setPersistenceManagerFactoryName(java.lang.String)
).This class also implements the
PersistenceExceptionTranslator
interface, as autodetected by Spring'sPersistenceExceptionTranslationPostProcessor
, for AOP-based translation of native exceptions to Spring DataAccessExceptions. Hence, the presence of a LocalPersistenceManagerFactoryBean automatically enables a PersistenceExceptionTranslationPostProcessor to translate JDO exceptions.Alternative: Configuration of a PersistenceManagerFactory provider bean
As alternative to the properties-driven approach that this FactoryBean offers (which is analogous to using the standard JDOHelper class with a Properties object that is populated with standard JDO properties), you can set up an instance of your PersistenceManagerFactory implementation class directly.
Like a DataSource, a PersistenceManagerFactory is encouraged to support bean-style configuration, which makes it very easy to set up as Spring-managed bean. The implementation class becomes the bean class; the remaining properties are applied as bean properties (starting with lower-case characters, in contrast to the corresponding JDO properties).
For example, in case of JPOX:
<bean id="persistenceManagerFactory" class="org.jpox.PersistenceManagerFactoryImpl" destroy-method="close"> <property name="connectionFactory" ref="dataSource"/> <property name="nontransactionalRead" value="true"/> </bean>
Note that such direct setup of a PersistenceManagerFactory implementation is the only way to pass an external connection factory (i.e. a JDBC DataSource) into a JDO PersistenceManagerFactory. With the standard properties-driven approach, you can only use an internal connection pool or a JNDI DataSource.
The
close()
method is standardized in JDO; don't forget to specify it as "destroy-method" for any PersistenceManagerFactory instance. Note that this FactoryBean will automatically invokeclose()
for the PersistenceManagerFactory that it creates, without any special configuration.- 从以下版本开始:
- 03.06.2003
- 作者:
- Juergen Hoeller
- 另请参阅:
JdoTransactionManager.setPersistenceManagerFactory(javax.jdo.PersistenceManagerFactory)
,JndiObjectFactoryBean
,JDOHelper.getPersistenceManagerFactory()
,PersistenceManagerFactory.setConnectionFactory(java.lang.Object)
,PersistenceManagerFactory.close()
,PersistenceExceptionTranslationPostProcessor
构造器概要
构造器 构造器 说明 LocalPersistenceManagerFactoryBean()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
afterPropertiesSet()
Initialize the PersistenceManagerFactory for the given location.void
destroy()
Close the PersistenceManagerFactory on bean factory shutdown.Map<String,Object>
getJdoPropertyMap()
Allow Map access to the JDO properties to be passed to the JDOHelper, with the option to add or override specific entries.javax.jdo.PersistenceManagerFactory
getObject()
Return the singleton PersistenceManagerFactory.Class<? extends javax.jdo.PersistenceManagerFactory>
getObjectType()
Return the type of object that this FactoryBean creates, ornull
if not known in advance.boolean
isSingleton()
Is the object managed by this factory a singleton?protected javax.jdo.PersistenceManagerFactory
newPersistenceManagerFactory(String name)
Subclasses can override this to perform custom initialization of the PersistenceManagerFactory instance, creating it for the specified name.protected javax.jdo.PersistenceManagerFactory
newPersistenceManagerFactory(Map<?,?> props)
Subclasses can override this to perform custom initialization of the PersistenceManagerFactory instance, creating it via the given Properties that got prepared by this LocalPersistenceManagerFactoryBean.void
setBeanClassLoader(ClassLoader beanClassLoader)
Callback that supplies the beanclass loader
to a bean instance.void
setConfigLocation(Resource configLocation)
Set the location of the JDO properties config file, for example as classpath resource "classpath:kodo.properties".void
setJdoDialect(JdoDialect jdoDialect)
Set the JDO dialect to use for the PersistenceExceptionTranslator functionality of this factory.void
setJdoProperties(Properties jdoProperties)
Set JDO properties, such as"javax.jdo.PersistenceManagerFactoryClass".void
setJdoPropertyMap(Map<String,Object> jdoProperties)
Specify JDO properties as a Map, to be passed intoJDOHelper.getPersistenceManagerFactory
(if any).void
setPersistenceManagerFactoryName(String persistenceManagerFactoryName)
Specify the name of the desired PersistenceManagerFactory.DataAccessException
translateExceptionIfPossible(RuntimeException ex)
Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.
构造器详细资料
LocalPersistenceManagerFactoryBean
public LocalPersistenceManagerFactoryBean()
方法详细资料
setPersistenceManagerFactoryName
public void setPersistenceManagerFactoryName(String persistenceManagerFactoryName)
Specify the name of the desired PersistenceManagerFactory.This may either be a properties resource in the classpath if such a resource exists, or a PMF definition with that name from "META-INF/jdoconfig.xml", or a JPA EntityManagerFactory cast to a PersistenceManagerFactory based on the persistence-unit name from "META-INF/persistence.xml" (JPA).
Default is none: Either 'persistenceManagerFactoryName' or 'configLocation' or 'jdoProperties' needs to be specified.
setConfigLocation
public void setConfigLocation(Resource configLocation)
Set the location of the JDO properties config file, for example as classpath resource "classpath:kodo.properties".Note: Can be omitted when all necessary properties are specified locally via this bean.
setJdoProperties
public void setJdoProperties(Properties jdoProperties)
Set JDO properties, such as"javax.jdo.PersistenceManagerFactoryClass".Can be used to override values in a JDO properties config file, or to specify all necessary properties locally.
Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.
setJdoPropertyMap
public void setJdoPropertyMap(Map<String,Object> jdoProperties)
Specify JDO properties as a Map, to be passed intoJDOHelper.getPersistenceManagerFactory
(if any).Can be populated with a "map" or "props" element in XML bean definitions.
- 另请参阅:
JDOHelper.getPersistenceManagerFactory(java.util.Map)
getJdoPropertyMap
public Map<String,Object> getJdoPropertyMap()
Allow Map access to the JDO properties to be passed to the JDOHelper, with the option to add or override specific entries.Useful for specifying entries directly, for example via "jdoPropertyMap[myKey]".
setJdoDialect
public void setJdoDialect(JdoDialect jdoDialect)
Set the JDO dialect to use for the PersistenceExceptionTranslator functionality of this factory.Default is a DefaultJdoDialect based on the PersistenceManagerFactory's underlying DataSource, if any.
setBeanClassLoader
public void setBeanClassLoader(ClassLoader beanClassLoader)
从接口复制的说明:BeanClassLoaderAware
Callback that supplies the beanclass loader
to a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as
InitializingBean's
InitializingBean.afterPropertiesSet()
method or a custom init-method.- 指定者:
setBeanClassLoader
在接口中BeanClassLoaderAware
- 参数:
beanClassLoader
- the owning class loader; may benull
in which case a defaultClassLoader
must be used, for example theClassLoader
obtained viaClassUtils.getDefaultClassLoader()
afterPropertiesSet
public void afterPropertiesSet() throws IllegalArgumentException, IOException, javax.jdo.JDOException
Initialize the PersistenceManagerFactory for the given location.- 指定者:
afterPropertiesSet
在接口中InitializingBean
- 抛出:
IllegalArgumentException
- in case of illegal property valuesIOException
- if the properties could not be loaded from the given locationjavax.jdo.JDOException
- in case of JDO initialization errors
newPersistenceManagerFactory
protected javax.jdo.PersistenceManagerFactory newPersistenceManagerFactory(String name)
Subclasses can override this to perform custom initialization of the PersistenceManagerFactory instance, creating it for the specified name.The default implementation invokes JDOHelper's
getPersistenceManagerFactory(String)
method. A custom implementation could prepare the instance in a specific way, or use a custom PersistenceManagerFactory implementation.- 参数:
name
- the name of the desired PersistenceManagerFactory- 返回:
- the PersistenceManagerFactory instance
- 另请参阅:
JDOHelper.getPersistenceManagerFactory(String)
newPersistenceManagerFactory
protected javax.jdo.PersistenceManagerFactory newPersistenceManagerFactory(Map<?,?> props)
Subclasses can override this to perform custom initialization of the PersistenceManagerFactory instance, creating it via the given Properties that got prepared by this LocalPersistenceManagerFactoryBean.The default implementation invokes JDOHelper's
getPersistenceManagerFactory(Map)
method. A custom implementation could prepare the instance in a specific way, or use a custom PersistenceManagerFactory implementation.- 参数:
props
- the merged properties prepared by this LocalPersistenceManagerFactoryBean- 返回:
- the PersistenceManagerFactory instance
- 另请参阅:
JDOHelper.getPersistenceManagerFactory(java.util.Map)
getObject
public javax.jdo.PersistenceManagerFactory getObject()
Return the singleton PersistenceManagerFactory.- 指定者:
getObject
在接口中FactoryBean<javax.jdo.PersistenceManagerFactory>
- 返回:
- an instance of the bean (can be
null
) - 另请参阅:
FactoryBeanNotInitializedException
getObjectType
public Class<? extends javax.jdo.PersistenceManagerFactory> getObjectType()
从接口复制的说明:FactoryBean
Return the type of object that this FactoryBean creates, ornull
if not known in advance.This allows one to check for specific types of beans without instantiating objects, for example on autowiring.
In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.
This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.
NOTE: Autowiring will simply ignore FactoryBeans that return
null
here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.- 指定者:
getObjectType
在接口中FactoryBean<javax.jdo.PersistenceManagerFactory>
- 返回:
- the type of object that this FactoryBean creates, or
null
if not known at the time of the call - 另请参阅:
ListableBeanFactory.getBeansOfType(java.lang.Class<T>)
isSingleton
public boolean isSingleton()
从接口复制的说明:FactoryBean
Is the object managed by this factory a singleton? That is, willFactoryBean.getObject()
always return the same object (a reference that can be cached)?NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from
getObject()
might get cached by the owning BeanFactory. Hence, do not returntrue
unless the FactoryBean always exposes the same reference.The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.
NOTE: This method returning
false
does not necessarily indicate that returned objects are independent instances. An implementation of the extendedSmartFactoryBean
interface may explicitly indicate independent instances through itsSmartFactoryBean.isPrototype()
method. PlainFactoryBean
implementations which do not implement this extended interface are simply assumed to always return independent instances if theisSingleton()
implementation returnsfalse
.- 指定者:
isSingleton
在接口中FactoryBean<javax.jdo.PersistenceManagerFactory>
- 返回:
- whether the exposed object is a singleton
- 另请参阅:
FactoryBean.getObject()
,SmartFactoryBean.isPrototype()
translateExceptionIfPossible
public DataAccessException translateExceptionIfPossible(RuntimeException ex)
Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.Converts the exception if it is a JDOException, preferably using a specified JdoDialect. Else returns
null
to indicate an unknown exception.- 指定者:
translateExceptionIfPossible
在接口中PersistenceExceptionTranslator
- 参数:
ex
- a RuntimeException to translate- 返回:
- the corresponding DataAccessException (or
null
if the exception could not be translated, as in this case it may result from user code rather than from an actual persistence problem) - 另请参阅:
PersistenceExceptionTranslationPostProcessor
,JdoDialect.translateException(javax.jdo.JDOException)
,PersistenceManagerFactoryUtils.convertJdoAccessException(javax.jdo.JDOException)
destroy
public void destroy()
Close the PersistenceManagerFactory on bean factory shutdown.- 指定者:
destroy
在接口中DisposableBean