how to register custom PersistenceAnnotationBeanPo

2020-07-06 07:23发布

I´d like to override the PersistenceAnnotationBeanPostProcessor which gets registered as soon as I insert a context:component-scan tag.

I tried to register a bean with the same name, but spring still registers the original postprocessor bean.

My goal is to provide an overriden version of findDefaultEntityManager method which will alow me to declare two EntityManagers in the same container.

Note: I have the 2 EM context running on spring 2.5.6, but it gets broken when migrated to 3.0.5.RELEASE version.

标签: spring orm jpa
2条回答
姐就是有狂的资本
2楼-- · 2020-07-06 07:36

Why not using the unitName attribute of your @PersistenceUnit/@PersistenceContext annotations? Since the javaDoc of PersistenceAnnotationBeanPostProcessor says it is supported...

查看更多
我想做一个坏孩纸
3楼-- · 2020-07-06 07:54

I found this workaround has worked for me ( Spring 3.2.7):

<bean id="org.springframework.context.annotation.internalPersistenceAnnotationProcessor"
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" >
<property name="defaultPersistenceUnitName" value="entityManagerFactory"/>
</bean> 

This would override the default PersistenceAnnotationBeanPostProcessor loaded by Spring with a new one with defaultPersistenceUnitName. You have to use the bean name Spring used when automatically loaded this bean (which is the default behaviour). I had to look into org.springframework.context.annotation.AnnotationConfigUtils.registerAnnotationConfigProcessors() to figure out this name.

查看更多
登录 后发表回答