Spring + EntityManagerFactory + Hibernate Listeners + Injection

I have a simple question. Is it possible to add dependency injection via @Ressource or @Autowired in the Hibernate Eventlistener?

I will show you my entitymanagerfactory configuration:

<bean id="entityManagerFactory" class="org.hibernate.ejb.EntityManagerFactoryImpl"> <qualifier value="entityManagerFactory" /> <constructor-arg> <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitManager"> <bean class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManagerr"> <property name="defaultDataSource" ref="dataSource" /> </bean> </property> <property name="dataSource" ref="dataSource" /> <property name="persistenceUnitName" value="mis" /> <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" /> <property name="jpaProperties" ref="jpa.properties" /> <property name="jpaDialect" ref="jpaDialect" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="true" /> <property name="database"> <util:constant static-field="org.springframework.orm.jpa.vendor.Database.POSTGRESQL" /> </property> <property name="showSql" value="true" /> </bean> </property> </bean> </constructor-arg> </bean> 

I am currently registering my listener through jpa.properties,

 hibernate.ejb.event.load=com.example.hibernate.events.LoadEvent 

but in this case I have no spring injection in my listener. I found a solution, but it uses sessionFactory, not entitymanager, or can I modify sessionfactory in my context? Hope someone has a good idea or solution to deal with this problem!

Thank you very much!

+8
java spring hibernate jpa hibernate-entitymanager
Nov 10 '10 at 11:17
source share
1 answer

If you used SessionFactory, this will be the configuration:

 <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- Stripped other stuff --> <property name="eventListeners"> <map> <entry key="pre-load"> <bean class="com.mycompany.MyCustomHibernateEventListener1" /> </entry> <entry key="pre-persist"> <bean class="com.mycompany.MyCustomHibernateEventListener2" /> </entry> </map> </property> </bean> 

But since you are using JPA, I am afraid that you need to use AOP as described in this thread

Or you can

  • store ApplicationContext in a ThreadLocal or user class of the owner and expose it through a static method
  • You have a base class for your listeners something like this:

Base class:

 public abstract class ListenerBase{ protected void wireMe(){ ApplicationContext ctx = ContextHelper.getCurrentApplicationContext(); ctx.getAutowireCapableBeanFactory().autowireBean(this); } } 

Now in your lifycycle methods, first call wireMe() .




Update:

Here is an example implementation of ContextHelper :

 public final class ContextHelper implements ApplicationContextAware{ private static final ContextHelper INSTANCE = new ContextHelper(); private ApplicationContext applicationContext; @Override public void setApplicationContext(final ApplicationContext applicationContext){ this.applicationContext = applicationContext; } public static ApplicationContext getCurrentApplicationContext(){ return INSTANCE.applicationContext; }; public static ContextHelper getInstance(){ return INSTANCE; } private ContextHelper(){ } } 

Connect it to your Spring Bean configuration as follows:

 <bean class="com.mycompany.ContextHelper" factory-method="getInstance" /> 
+16
Nov 10 2018-10-11
source share



All Articles