sping data jpa 如何注入的entitymanager

 

public class JpaRepositoryFactoryBean<T extends JpaRepository<S, ID>, S, ID extends Serializable> extends

		TransactionalRepositoryFactoryBeanSupport<T, S, ID> {



	private EntityManager entityManager;



	/**

	 * The {@link EntityManager} to be used.

	 * 

	 * @param entityManager the entityManager to set

	 */

	@PersistenceContext

	public void setEntityManager(EntityManager entityManager) {



		this.entityManager = entityManager;

	}


可以看到entityManager是通过PersistenceContext注解获取的。

2.如果要是希望在spring环境中使用@PersistenceContext和@PersistenceUnit,则在application.xml中配置

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

特别注意的是 :如果你在application.xml中配置过context:annotation-config" 或者 "context:component-scan tag,则会默认register一个默认的PersistenceAnnotationBeanPostProcessor。所以如果你要是希望自定义PersistenceAnnotationBeanPostProcessor。则Remove or turn off the default annotation configuration,具体怎么操作,正在研究,不好意思

可以通过这个类指定多个"persistenceUnits"

 <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>

 也可以指定多个persistenceContexts

 <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>

 

你可能感兴趣的:(manager)