Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2002-2018 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.support;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.PersistenceProperty;
import javax.persistence.PersistenceUnit;
import javax.persistence.SynchronizationType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.InjectionMetadata;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.config.NamedBeanHolder;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.jndi.JndiLocatorDelegate;
import org.springframework.jndi.JndiTemplate;
import org.springframework.lang.Nullable;
import org.springframework.orm.jpa.EntityManagerFactoryInfo;
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
import org.springframework.orm.jpa.EntityManagerProxy;
import org.springframework.orm.jpa.ExtendedEntityManagerCreator;
import org.springframework.orm.jpa.SharedEntityManagerCreator;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
/**
* BeanPostProcessor that processes {@link javax.persistence.PersistenceUnit}
* and {@link javax.persistence.PersistenceContext} annotations, for injection of
* the corresponding JPA resources {@link javax.persistence.EntityManagerFactory}
* and {@link javax.persistence.EntityManager}. Any such annotated fields or methods
* in any Spring-managed object will automatically be injected.
*
*
This post-processor will inject sub-interfaces of {@code EntityManagerFactory}
* and {@code 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")
* {@code EntityManager} reference, where type mismatches might be detected as late
* as on the first actual invocation.
*
*
Note: In the present implementation, PersistenceAnnotationBeanPostProcessor
* only supports {@code @PersistenceUnit} and {@code @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 {@link org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean}
* will be used for setting up such EntityManagerFactory beans. Alternatively,
* such beans may also be obtained from JNDI, e.g. using the {@code jee: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:
*
*
*
* In the JNDI case, specify the corresponding JNDI names in this post-processor's
* {@link #setPersistenceUnits "persistenceUnits" map}, typically with matching
* {@code 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:
*
*
*
* 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 {@code @Transactional} annotation for demarcation
* and {@link org.springframework.transaction.jta.JtaTransactionManager} as backend).
*
*
If you prefer the Java EE server's own EntityManager handling, specify entries
* in this post-processor's {@link #setPersistenceContexts "persistenceContexts" map}
* (or {@link #setExtendedPersistenceContexts "extendedPersistenceContexts" map},
* typically with matching {@code persistence-context-ref} entries in the
* Java EE deployment descriptor. For example:
*
*
*
* 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 {@code @PersistenceContext} with type {@code 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).
*
*
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.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @since 2.0
* @see javax.persistence.PersistenceUnit
* @see javax.persistence.PersistenceContext
*/
@SuppressWarnings("serial")
public class PersistenceAnnotationBeanPostProcessor
implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor,
MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware, Serializable {
@Nullable
private Object jndiEnvironment;
private boolean resourceRef = true;
@Nullable
private transient Map persistenceUnits;
@Nullable
private transient Map persistenceContexts;
@Nullable
private transient Map extendedPersistenceContexts;
private transient String defaultPersistenceUnitName = "";
private int order = Ordered.LOWEST_PRECEDENCE - 4;
@Nullable
private transient ListableBeanFactory beanFactory;
private final transient Map injectionMetadataCache = new ConcurrentHashMap<>(256);
private final Map