2010-11-10 70 views
8

我有一個簡單的問題。它可能通過@Ressource或@Autowired添加到Hibernate Eventlistener的依賴注入?Spring + EntityManagerFactory +休眠監聽器+注入

我會告訴你我的EntityManagerFactory配置:

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

目前我註冊通過jpa.properties我的聽衆,

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

但在這種情況下,我有我的聽衆沒有春天注入。我找到了一個解決方案,但是這使用sessionFactory而不是entitymanager或者我可以在我的上下文中修改sessionfactory嗎?希望有人有一個好主意或解決方案來處理這個問題!

非常感謝!

回答

16

如果您使用的SessionFactory,這將是配置:

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

但因爲你正在使用JPA,恐怕你需要儘可能列出in this thread

或者你可以

使用AOP
  1. 將ApplicationContext存儲在ThreadLocal或自定義持有者類中並通過靜態方法公開它
  2. 有一個基類爲你的聽衆是這樣的:

基類:

public abstract class ListenerBase{ 

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

} 

現在,在您lifycycle方法調用wireMe()第一。


更新:

這裏是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(){ 
    } 

} 

線在你的Spring bean的配置是這樣的:

<bean class="com.mycompany.ContextHelper" factory-method="getInstance" /> 
+0

你好,謝謝你的輸入反應。你能告訴我在哪裏找到ContextHelper?我有一個來自Hibernate.search.util。和Hibernate.search.event,並且沒有方法「getCurrentApplicationContext()」 – moohkooh 2010-11-11 07:49:49

+0

這是您需要創建的類。我現在附上了一個示例版本。 – 2010-11-11 08:06:47

+0

非常感謝,它很有用。我不能告訴你,你幫了我多少!非常感謝! – moohkooh 2010-11-11 08:20:51