首页 文章

Spring没有使用抽象超类将DAO注入到JSF托管bean中

提问于
浏览
1

我有一个JSF 2.0应用程序,我正在集成Spring,所以我可以使用hibernateTemplate . 我已经就JSF集成咨询了Spring documentation并已采取措施进行设置 . 我的所有bean类都扩展了一个名为SuperBean的抽象超类 . SuperBean是理想的注入点,使我不必在Spring中声明所有的bean . 我希望将它声明为abstract = "true",并且任何扩展SuperBean类的子类bean都会注入dao . 在运行时它是null .

<bean id="serviceTemplate" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
   <property name="transactionManager" ref="transactionManager"/>
   <property name="transactionAttributes">
      <props>
         <prop key="*"/>
      </props>
   </property>
</bean>
<bean id="daoServiceTarget" class="com.example.service.DaoService">
       <property name="mainDAO" ref="mainDAO"/>
    </bean>
    <bean id="daoService" parent="serviceTemplate">
       <property name="target" ref="daoServiceTarget"/>
    </bean>    
    <bean id="superBean" class="com.example.beans.SuperBean" abstract="true">
       <property name="daoService" ref="daoService"/>
    </bean>

我能简单地声明这个超类SuperBean并期望Spring注入dao吗?我不想在 Spring 天声明每个bean类 .

我认为替代选项(从性能角度来看)将不使用Spring bean,而是将DAO声明为@applicationScoped并使用JEE的CDI将它们注入SuperBean类 . 这会更好地表现吗?

1 回答

  • 1

    在上面的示例中,它看起来像serviceTemplate提供了您想要的示例 . 注意parent = "serviceTemplate" . 你需要做一些类似于从superbean继承的东西 . 还有其他选项,但是因为你在serviceTemplate中有工作代码可能是最好的起点 . 然后阅读here了解更多详情:

相关问题