Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to register custom PersistenceAnnotationBeanPostProcessor in spring 3.0

Tags:

spring

orm

jpa

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.

like image 222
Luiz Henrique Martins Lins Rol Avatar asked May 31 '11 03:05

Luiz Henrique Martins Lins Rol


1 Answers

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.

like image 56
mrocabado Avatar answered Oct 27 '22 00:10

mrocabado