类 PersistenceAnnotationBeanPostProcessor
- java.lang.Object
- org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor
- 所有已实现的接口:
Serializable
,Aware
,BeanFactoryAware
,BeanPostProcessor
,DestructionAwareBeanPostProcessor
,InstantiationAwareBeanPostProcessor
,MergedBeanDefinitionPostProcessor
,Ordered
,PriorityOrdered
public class PersistenceAnnotationBeanPostProcessor extends Object implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware, Serializable
BeanPostProcessor that processesPersistenceUnit
andPersistenceContext
annotations, for injection of the corresponding JPA resourcesEntityManagerFactory
andEntityManager
. Any such annotated fields or methods in any Spring-managed object will automatically be injected.This post-processor will inject sub-interfaces of
EntityManagerFactory
andEntityManager
if the annotated fields or methods are declared as such. The actual type will be verified early, with the exception of a shared ("transactional")EntityManager
reference, where type mismatches might be detected as late as on the first actual invocation.Note: In the present implementation, PersistenceAnnotationBeanPostProcessor only supports
@PersistenceUnit
and@PersistenceContext
with the "unitName" attribute, or no attribute at all (for the default unit). If those annotations are present with the "name" attribute at the class level, they will simply be ignored, since those only serve as deployment hint (as per the Java EE specification).This post-processor can either obtain EntityManagerFactory beans defined in the Spring application context (the default), or obtain EntityManagerFactory references from JNDI ("persistence unit references"). In the bean case, the persistence unit name will be matched against the actual deployed unit, with the bean name used as fallback unit name if no deployed name found. Typically, Spring's
LocalContainerEntityManagerFactoryBean
will be used for setting up such EntityManagerFactory beans. Alternatively, such beans may also be obtained from JNDI, e.g. using thejee:jndi-lookup
XML configuration element (with the bean name matching the requested unit name). In both cases, the post-processor definition will look as simple as this:<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
In the JNDI case, specify the corresponding JNDI names in this post-processor's"persistenceUnits" map
, typically with matchingpersistence-unit-ref
entries in the Java EE deployment descriptor. By default, those names are considered as resource references (according to the Java EE resource-ref convention), located underneath the "java:comp/env/" namespace. For example:<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"> <property name="persistenceUnits"> <map/gt; <entry key="unit1" value="persistence/unit1"/> <entry key="unit2" value="persistence/unit2"/> </map/gt; </property> </bean>
In this case, the specified persistence units will always be resolved in JNDI rather than as Spring-defined beans. The entire persistence unit deployment, including the weaving of persistent classes, is then up to the Java EE server. Persistence contexts (i.e. EntityManager references) will be built based on those server-provided EntityManagerFactory references, using Spring's own transaction synchronization facilities for transactional EntityManager handling (typically with Spring's@Transactional
annotation for demarcation andJtaTransactionManager
as backend).If you prefer the Java EE server's own EntityManager handling, specify entries in this post-processor's
"persistenceContexts" map
(or"extendedPersistenceContexts" map
, typically with matchingpersistence-context-ref
entries in the Java EE deployment descriptor. For example:<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"> <property name="persistenceContexts"> <map/gt; <entry key="unit1" value="persistence/context1"/> <entry key="unit2" value="persistence/context2"/> </map/gt; </property> </bean>
If the application only obtains EntityManager references in the first place, this is all you need to specify. If you need EntityManagerFactory references as well, specify entries for both "persistenceUnits" and "persistenceContexts", pointing to matching JNDI locations.NOTE: In general, do not inject EXTENDED EntityManagers into STATELESS beans, i.e. do not use
@PersistenceContext
with typeEXTENDED
in Spring beans defined with scope 'singleton' (Spring's default scope). Extended EntityManagers are not thread-safe, hence they must not be used in concurrently accessed beans (which Spring-managed singletons usually are).Note: A default PersistenceAnnotationBeanPostProcessor will be registered by the "context:annotation-config" and "context:component-scan" XML tags. Remove or turn off the default annotation configuration there if you intend to specify a custom PersistenceAnnotationBeanPostProcessor bean definition.
- 从以下版本开始:
- 2.0
- 作者:
- Rod Johnson, Juergen Hoeller
- 另请参阅:
PersistenceUnit
,PersistenceContext
, 序列化表格
字段概要
从接口继承的字段 org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
构造器概要
构造器 构造器 说明 PersistenceAnnotationBeanPostProcessor()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected EntityManagerFactory
findDefaultEntityManagerFactory(String requestingBeanName)
Find a single default EntityManagerFactory in the Spring application context.protected EntityManagerFactory
findEntityManagerFactory(String unitName, String requestingBeanName)
Find an EntityManagerFactory with the given name in the current Spring application context, falling back to a single default EntityManagerFactory (if any) in case of no unit name specified.protected EntityManagerFactory
findNamedEntityManagerFactory(String unitName, String requestingBeanName)
Find an EntityManagerFactory with the given name in the current Spring application context.int
getOrder()
Get the order value of this object.protected EntityManager
getPersistenceContext(String unitName, boolean extended)
Return a specified persistence context for the given unit name, as defined through the "persistenceContexts" (or "extendedPersistenceContexts") map.protected EntityManagerFactory
getPersistenceUnit(String unitName)
Return a specified persistence unit for the given unit name, as defined through the "persistenceUnits" map.protected <T> T
lookup(String jndiName, Class<T> requiredType)
Perform a JNDI lookup for the given resource by name.Object
postProcessAfterInitialization(Object bean, String beanName)
Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean'safterPropertiesSet
or a custom init-method).boolean
postProcessAfterInstantiation(Object bean, String beanName)
Perform operations after the bean has been instantiated, via a constructor or factory method, but before Spring property population (from explicit properties or autowiring) occurs.void
postProcessBeforeDestruction(Object bean, String beanName)
Apply this BeanPostProcessor to the given bean instance before its destruction, e.g. invoking custom destruction callbacks.Object
postProcessBeforeInitialization(Object bean, String beanName)
Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean'safterPropertiesSet
or a custom init-method).Object
postProcessBeforeInstantiation(Class<?> beanClass, String beanName)
Apply this BeanPostProcessor before the target bean gets instantiated.void
postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName)
Post-process the given merged bean definition for the specified bean.PropertyValues
postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
Post-process the given property values before the factory applies them to the given bean.boolean
requiresDestruction(Object bean)
Determine whether the given bean instance requires destruction by this post-processor.void
setBeanFactory(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance.void
setDefaultPersistenceUnitName(String unitName)
Specify the default persistence unit name, to be used in case of no unit name specified in an@PersistenceUnit
/@PersistenceContext
annotation.void
setExtendedPersistenceContexts(Map<String,String> extendedPersistenceContexts)
Specify the extended persistence contexts for EntityManager lookups, as a Map from persistence unit name to persistence context JNDI name (which needs to resolve to an EntityManager instance).void
setJndiEnvironment(Properties jndiEnvironment)
Set the JNDI environment to use for JNDI lookups.void
setJndiTemplate(Object jndiTemplate)
Set the JNDI template to use for JNDI lookups.void
setOrder(int order)
void
setPersistenceContexts(Map<String,String> persistenceContexts)
Specify the transactional persistence contexts for EntityManager lookups, as a Map from persistence unit name to persistence context JNDI name (which needs to resolve to an EntityManager instance).void
setPersistenceUnits(Map<String,String> persistenceUnits)
Specify the persistence units for EntityManagerFactory lookups, as a Map from persistence unit name to persistence unit JNDI name (which needs to resolve to an EntityManagerFactory instance).void
setResourceRef(boolean resourceRef)
Set whether the lookup occurs in a J2EE container, i.e. if the prefix "java:comp/env/" needs to be added if the JNDI name doesn't already contain it.
构造器详细资料
PersistenceAnnotationBeanPostProcessor
public PersistenceAnnotationBeanPostProcessor()
方法详细资料
setJndiTemplate
public void setJndiTemplate(Object jndiTemplate)
Set the JNDI template to use for JNDI lookups.
setJndiEnvironment
public void setJndiEnvironment(Properties jndiEnvironment)
Set the JNDI environment to use for JNDI lookups.
setResourceRef
public void setResourceRef(boolean resourceRef)
Set whether the lookup occurs in a J2EE container, i.e. if the prefix "java:comp/env/" needs to be added if the JNDI name doesn't already contain it. PersistenceAnnotationBeanPostProcessor's default is "true".
setPersistenceUnits
public void setPersistenceUnits(Map<String,String> persistenceUnits)
Specify the persistence units for EntityManagerFactory lookups, as a Map from persistence unit name to persistence unit JNDI name (which needs to resolve to an EntityManagerFactory instance).JNDI names specified here should refer to
persistence-unit-ref
entries in the Java EE deployment descriptor, matching the target persistence unit.In case of no unit name specified in the annotation, the specified value for the
default persistence unit
will be taken (by default, the value mapped to the empty String), or simply the single persistence unit if there is only one.This is mainly intended for use in a Java EE environment, with all lookup driven by the standard JPA annotations, and all EntityManagerFactory references obtained from JNDI. No separate EntityManagerFactory bean definitions are necessary in such a scenario.
If no corresponding "persistenceContexts"/"extendedPersistenceContexts" are specified,
@PersistenceContext
will be resolved to EntityManagers built on top of the EntityManagerFactory defined here. Note that those will be Spring-managed EntityManagers, which implement transaction synchronization based on Spring's facilities. If you prefer the Java EE server's own EntityManager handling, specify corresponding "persistenceContexts"/"extendedPersistenceContexts".
setPersistenceContexts
public void setPersistenceContexts(Map<String,String> persistenceContexts)
Specify the transactional persistence contexts for EntityManager lookups, as a Map from persistence unit name to persistence context JNDI name (which needs to resolve to an EntityManager instance).JNDI names specified here should refer to
persistence-context-ref
entries in the Java EE deployment descriptors, matching the target persistence unit and being set up with persistence context typeTransaction
.In case of no unit name specified in the annotation, the specified value for the
default persistence unit
will be taken (by default, the value mapped to the empty String), or simply the single persistence unit if there is only one.This is mainly intended for use in a Java EE environment, with all lookup driven by the standard JPA annotations, and all EntityManager references obtained from JNDI. No separate EntityManagerFactory bean definitions are necessary in such a scenario, and all EntityManager handling is done by the Java EE server itself.
setExtendedPersistenceContexts
public void setExtendedPersistenceContexts(Map<String,String> extendedPersistenceContexts)
Specify the extended persistence contexts for EntityManager lookups, as a Map from persistence unit name to persistence context JNDI name (which needs to resolve to an EntityManager instance).JNDI names specified here should refer to
persistence-context-ref
entries in the Java EE deployment descriptors, matching the target persistence unit and being set up with persistence context typeExtended
.In case of no unit name specified in the annotation, the specified value for the
default persistence unit
will be taken (by default, the value mapped to the empty String), or simply the single persistence unit if there is only one.This is mainly intended for use in a Java EE environment, with all lookup driven by the standard JPA annotations, and all EntityManager references obtained from JNDI. No separate EntityManagerFactory bean definitions are necessary in such a scenario, and all EntityManager handling is done by the Java EE server itself.
setDefaultPersistenceUnitName
public void setDefaultPersistenceUnitName(String unitName)
Specify the default persistence unit name, to be used in case of no unit name specified in an@PersistenceUnit
/@PersistenceContext
annotation.This is mainly intended for lookups in the application context, indicating the target persistence unit name (typically matching the bean name), but also applies to lookups in the
"persistenceUnits"
/"persistenceContexts"
/"extendedPersistenceContexts"
map, avoiding the need for duplicated mappings for the empty String there.Default is to check for a single EntityManagerFactory bean in the Spring application context, if any. If there are multiple such factories, either specify this default persistence unit name or explicitly refer to named persistence units in your annotations.
setOrder
public void setOrder(int order)
getOrder
public int getOrder()
从接口复制的说明:Ordered
Get the order value of this object.Higher values are interpreted as lower priority. As a consequence, the object with the lowest value has the highest priority (somewhat analogous to Servlet
load-on-startup
values).Same order values will result in arbitrary sort positions for the affected objects.
- 指定者:
getOrder
在接口中Ordered
- 返回:
- the order value
- 另请参阅:
Ordered.HIGHEST_PRECEDENCE
,Ordered.LOWEST_PRECEDENCE
setBeanFactory
public void setBeanFactory(BeanFactory beanFactory)
从接口复制的说明:BeanFactoryAware
Callback that supplies the owning factory to a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.- 指定者:
setBeanFactory
在接口中BeanFactoryAware
- 参数:
beanFactory
- owning BeanFactory (nevernull
). The bean can immediately call methods on the factory.- 另请参阅:
BeanInitializationException
postProcessMergedBeanDefinition
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName)
从接口复制的说明:MergedBeanDefinitionPostProcessor
Post-process the given merged bean definition for the specified bean.- 指定者:
postProcessMergedBeanDefinition
在接口中MergedBeanDefinitionPostProcessor
- 参数:
beanDefinition
- the merged bean definition for the beanbeanType
- the actual type of the managed bean instancebeanName
- the name of the bean
postProcessBeforeInstantiation
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException
从接口复制的说明:InstantiationAwareBeanPostProcessor
Apply this BeanPostProcessor before the target bean gets instantiated. The returned bean object may be a proxy to use instead of the target bean, effectively suppressing default instantiation of the target bean.If a non-null object is returned by this method, the bean creation process will be short-circuited. The only further processing applied is the
BeanPostProcessor.postProcessAfterInitialization(java.lang.Object, java.lang.String)
callback from the configuredBeanPostProcessors
.This callback will be applied to bean definitions with their bean class, as well as to factory-method definitions in which case the returned bean type will be passed in here.
Post-processors may implement the extended
SmartInstantiationAwareBeanPostProcessor
interface in order to predict the type of the bean object that they are going to return here.- 指定者:
postProcessBeforeInstantiation
在接口中InstantiationAwareBeanPostProcessor
- 参数:
beanClass
- the class of the bean to be instantiatedbeanName
- the name of the bean- 返回:
- the bean object to expose instead of a default instance of the target bean, or
null
to proceed with default instantiation - 抛出:
BeansException
- in case of errors- 另请参阅:
InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation(java.lang.Object, java.lang.String)
,AbstractBeanDefinition.getBeanClass()
,AbstractBeanDefinition.getFactoryMethodName()
postProcessAfterInstantiation
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException
从接口复制的说明:InstantiationAwareBeanPostProcessor
Perform operations after the bean has been instantiated, via a constructor or factory method, but before Spring property population (from explicit properties or autowiring) occurs.This is the ideal callback for performing custom field injection on the given bean instance, right before Spring's autowiring kicks in.
- 指定者:
postProcessAfterInstantiation
在接口中InstantiationAwareBeanPostProcessor
- 参数:
bean
- the bean instance created, with properties not having been set yetbeanName
- the name of the bean- 返回:
true
if properties should be set on the bean;false
if property population should be skipped. Normal implementations should returntrue
. Returningfalse
will also prevent any subsequent InstantiationAwareBeanPostProcessor instances being invoked on this bean instance.- 抛出:
BeansException
- in case of errors- 另请参阅:
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)
postProcessPropertyValues
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException
从接口复制的说明:InstantiationAwareBeanPostProcessor
Post-process the given property values before the factory applies them to the given bean. Allows for checking whether all dependencies have been satisfied, for example based on a "Required" annotation on bean property setters.Also allows for replacing the property values to apply, typically through creating a new MutablePropertyValues instance based on the original PropertyValues, adding or removing specific values.
- 指定者:
postProcessPropertyValues
在接口中InstantiationAwareBeanPostProcessor
- 参数:
pvs
- the property values that the factory is about to apply (nevernull
)pds
- the relevant property descriptors for the target bean (with ignored dependency types - which the factory handles specifically - already filtered out)bean
- the bean instance created, but whose properties have not yet been setbeanName
- the name of the bean- 返回:
- the actual property values to apply to the given bean (can be the passed-in PropertyValues instance), or
null
to skip property population - 抛出:
BeansException
- in case of errors- 另请参阅:
MutablePropertyValues
postProcessBeforeInitialization
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
从接口复制的说明:BeanPostProcessor
Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean'safterPropertiesSet
or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.- 指定者:
postProcessBeforeInitialization
在接口中BeanPostProcessor
- 参数:
bean
- the new bean instancebeanName
- the name of the bean- 返回:
- the bean instance to use, either the original or a wrapped one; if
null
, no subsequent BeanPostProcessors will be invoked - 抛出:
BeansException
- in case of errors- 另请参阅:
InitializingBean.afterPropertiesSet()
postProcessAfterInitialization
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
从接口复制的说明:BeanPostProcessor
Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean'safterPropertiesSet
or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.In case of a FactoryBean, this callback will be invoked for both the FactoryBean instance and the objects created by the FactoryBean (as of Spring 2.0). The post-processor can decide whether to apply to either the FactoryBean or created objects or both through corresponding
bean instanceof FactoryBean
checks.This callback will also be invoked after a short-circuiting triggered by a
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)
method, in contrast to all other BeanPostProcessor callbacks.- 指定者:
postProcessAfterInitialization
在接口中BeanPostProcessor
- 参数:
bean
- the new bean instancebeanName
- the name of the bean- 返回:
- the bean instance to use, either the original or a wrapped one; if
null
, no subsequent BeanPostProcessors will be invoked - 抛出:
BeansException
- in case of errors- 另请参阅:
InitializingBean.afterPropertiesSet()
,FactoryBean
postProcessBeforeDestruction
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException
从接口复制的说明:DestructionAwareBeanPostProcessor
Apply this BeanPostProcessor to the given bean instance before its destruction, e.g. invoking custom destruction callbacks.Like DisposableBean's
destroy
and a custom destroy method, this callback will only apply to beans which the container fully manages the lifecycle for. This is usually the case for singletons and scoped beans.- 指定者:
postProcessBeforeDestruction
在接口中DestructionAwareBeanPostProcessor
- 参数:
bean
- the bean instance to be destroyedbeanName
- the name of the bean- 抛出:
BeansException
- in case of errors- 另请参阅:
DisposableBean.destroy()
,AbstractBeanDefinition.setDestroyMethodName(String)
requiresDestruction
public boolean requiresDestruction(Object bean)
从接口复制的说明:DestructionAwareBeanPostProcessor
Determine whether the given bean instance requires destruction by this post-processor.NOTE: Even as a late addition, this method has been introduced on
DestructionAwareBeanPostProcessor
itself instead of on a SmartDABPP subinterface. This allows existingDestructionAwareBeanPostProcessor
implementations to easily providerequiresDestruction
logic while retaining compatibility with Spring <4.3, and it is also an easier onramp to declaringrequiresDestruction
as a Java 8 default method in Spring 5.If an implementation of
DestructionAwareBeanPostProcessor
does not provide a concrete implementation of this method, Spring's invocation mechanism silently assumes a method returningtrue
(the effective default before 4.3, and the to-be-default in the Java 8 method in Spring 5).- 指定者:
requiresDestruction
在接口中DestructionAwareBeanPostProcessor
- 参数:
bean
- the bean instance to check- 返回:
true
ifDestructionAwareBeanPostProcessor.postProcessBeforeDestruction(java.lang.Object, java.lang.String)
is supposed to be called for this bean instance eventually, orfalse
if not needed
getPersistenceUnit
protected EntityManagerFactory getPersistenceUnit(String unitName)
Return a specified persistence unit for the given unit name, as defined through the "persistenceUnits" map.- 参数:
unitName
- the name of the persistence unit- 返回:
- the corresponding EntityManagerFactory, or
null
if none found - 另请参阅:
setPersistenceUnits(java.util.Map<java.lang.String, java.lang.String>)
getPersistenceContext
protected EntityManager getPersistenceContext(String unitName, boolean extended)
Return a specified persistence context for the given unit name, as defined through the "persistenceContexts" (or "extendedPersistenceContexts") map.- 参数:
unitName
- the name of the persistence unitextended
- whether to obtain an extended persistence context- 返回:
- the corresponding EntityManager, or
null
if none found - 另请参阅:
setPersistenceContexts(java.util.Map<java.lang.String, java.lang.String>)
,setExtendedPersistenceContexts(java.util.Map<java.lang.String, java.lang.String>)
findEntityManagerFactory
protected EntityManagerFactory findEntityManagerFactory(String unitName, String requestingBeanName) throws NoSuchBeanDefinitionException
Find an EntityManagerFactory with the given name in the current Spring application context, falling back to a single default EntityManagerFactory (if any) in case of no unit name specified.- 参数:
unitName
- the name of the persistence unit (may benull
or empty)requestingBeanName
- the name of the requesting bean- 返回:
- the EntityManagerFactory
- 抛出:
NoSuchBeanDefinitionException
- if there is no such EntityManagerFactory in the context
findNamedEntityManagerFactory
protected EntityManagerFactory findNamedEntityManagerFactory(String unitName, String requestingBeanName) throws NoSuchBeanDefinitionException
Find an EntityManagerFactory with the given name in the current Spring application context.- 参数:
unitName
- the name of the persistence unit (never empty)requestingBeanName
- the name of the requesting bean- 返回:
- the EntityManagerFactory
- 抛出:
NoSuchBeanDefinitionException
- if there is no such EntityManagerFactory in the context
findDefaultEntityManagerFactory
protected EntityManagerFactory findDefaultEntityManagerFactory(String requestingBeanName) throws NoSuchBeanDefinitionException
Find a single default EntityManagerFactory in the Spring application context.- 返回:
- the default EntityManagerFactory
- 抛出:
NoSuchBeanDefinitionException
- if there is no single EntityManagerFactory in the context
lookup
protected <T> T lookup(String jndiName, Class<T> requiredType) throws Exception
Perform a JNDI lookup for the given resource by name.Called for EntityManagerFactory and EntityManager lookup when JNDI names are mapped for specific persistence units.
- 参数:
jndiName
- the JNDI name to look uprequiredType
- the required type of the object- 返回:
- the obtained object
- 抛出:
Exception
- if the JNDI lookup failed