|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.springframework.jndi.JndiAccessor
org.springframework.jndi.JndiLocatorSupport
org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor
public class PersistenceAnnotationBeanPostProcessor
BeanPostProcessor that processes PersistenceUnit
and PersistenceContext
annotations, for injection of
the corresponding JPA resources EntityManagerFactory
and EntityManager
. Any such annotated fields or methods
in any Spring-managed object will automatically be injected.
This post-processor will inject sub-interfaces of EntityManagerFactory
and EntityManager
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 5 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. The post-processor
definition will then 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 matching
persistence-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
and JtaTransactionManager
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 matching persistence-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 type EXTENDED
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).
PersistenceUnit
,
PersistenceContext
Field Summary | |
---|---|
protected Log |
logger
|
Fields inherited from class org.springframework.jndi.JndiLocatorSupport |
---|
CONTAINER_PREFIX |
Constructor Summary | |
---|---|
PersistenceAnnotationBeanPostProcessor()
|
Method Summary | |
---|---|
protected javax.persistence.EntityManagerFactory |
findDefaultEntityManagerFactory()
Find a single default EntityManagerFactory in the Spring application context. |
protected javax.persistence.EntityManagerFactory |
findEntityManagerFactory(String unitName)
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 javax.persistence.EntityManagerFactory |
findNamedEntityManagerFactory(String unitName)
Find an EntityManagerFactory with the given name in the current Spring application context. |
protected javax.persistence.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 javax.persistence.EntityManagerFactory |
getPersistenceUnit(String unitName)
Return a specified persistence unit for the given unit name, as defined through the "persistenceUnits" map. |
Object |
postProcessAfterInitialization(Object bean,
String beanName)
Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean's afterPropertiesSet
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. |
Object |
postProcessBeforeInitialization(Object bean,
String beanName)
Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet
or a custom init-method). |
Object |
postProcessBeforeInstantiation(Class beanClass,
String beanName)
Apply this BeanPostProcessor before the target bean gets instantiated. |
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. |
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 |
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). |
Methods inherited from class org.springframework.jndi.JndiLocatorSupport |
---|
convertJndiName, isResourceRef, lookup, lookup, setResourceRef |
Methods inherited from class org.springframework.jndi.JndiAccessor |
---|
getJndiEnvironment, getJndiTemplate, setJndiEnvironment, setJndiTemplate |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final Log logger
Constructor Detail |
---|
public PersistenceAnnotationBeanPostProcessor()
Method Detail |
---|
public void setPersistenceUnits(Map<String,String> persistenceUnits)
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 5 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 5 server's own EntityManager handling,
specify corresponding "persistenceContexts"/"extendedPersistenceContexts".
public void setPersistenceContexts(Map<String,String> persistenceContexts)
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 type Transaction
.
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 5 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 5 server itself.
public void setExtendedPersistenceContexts(Map<String,String> extendedPersistenceContexts)
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 type Extended
.
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 5 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 5 server itself.
public void setDefaultPersistenceUnitName(String unitName)
@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.
public void setBeanFactory(BeanFactory beanFactory)
BeanFactoryAware
Invoked after the population of normal bean properties
but before an initialization callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.BeanInitializationException
public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException
InstantiationAwareBeanPostProcessor
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 configured
BeanPostProcessors
.
This callback will only be applied to bean definitions with a bean class. In particular, it will not be applied to beans with a "factory-method".
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
in interface InstantiationAwareBeanPostProcessor
beanClass
- the class of the bean to be instantiatedbeanName
- the name of the bean
null
to proceed with default instantiation
BeansException
- in case of errorsAbstractBeanDefinition.hasBeanClass()
,
AbstractBeanDefinition.getFactoryMethodName()
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException
InstantiationAwareBeanPostProcessor
postProcessAfterInstantiation
in interface InstantiationAwareBeanPostProcessor
bean
- the bean instance created, but whose properties have not yet been setbeanName
- the name of the bean
true
if properties should be set on the bean; false
if property population should be skipped. Normal implementations should return true
.
Returning false
will also prevent any subsequent InstantiationAwareBeanPostProcessor
instances being invoked on this bean instance.
BeansException
- in case of errorspublic PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException
InstantiationAwareBeanPostProcessor
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
in interface InstantiationAwareBeanPostProcessor
pvs
- the property values that the factory is about to apply (never null
)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
null
to skip property population
BeansException
- in case of errorsMutablePropertyValues
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
BeanPostProcessor
afterPropertiesSet
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
in interface BeanPostProcessor
bean
- the new bean instancebeanName
- the name of the bean
BeansException
- in case of errorsInitializingBean.afterPropertiesSet()
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
BeanPostProcessor
afterPropertiesSet
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
in interface BeanPostProcessor
bean
- the new bean instancebeanName
- the name of the bean
BeansException
- in case of errorsInitializingBean.afterPropertiesSet()
,
FactoryBean
protected javax.persistence.EntityManagerFactory getPersistenceUnit(String unitName)
unitName
- the name of the persistence unit
null
if none foundsetPersistenceUnits(java.util.Map)
protected javax.persistence.EntityManager getPersistenceContext(String unitName, boolean extended)
unitName
- the name of the persistence unitextended
- whether to obtain an extended persistence context
null
if none foundsetPersistenceContexts(java.util.Map)
,
setExtendedPersistenceContexts(java.util.Map)
protected javax.persistence.EntityManagerFactory findEntityManagerFactory(String unitName) throws NoSuchBeanDefinitionException
unitName
- the name of the persistence unit (may be null
or empty)
NoSuchBeanDefinitionException
- if there is no such EntityManagerFactory in the contextprotected javax.persistence.EntityManagerFactory findNamedEntityManagerFactory(String unitName) throws NoSuchBeanDefinitionException
unitName
- the name of the persistence unit (never empty)
NoSuchBeanDefinitionException
- if there is no such EntityManagerFactory in the contextprotected javax.persistence.EntityManagerFactory findDefaultEntityManagerFactory()
NoSuchBeanDefinitionException
- if there is no single EntityManagerFactory in the context
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |