2012-07-12 46 views
2

试图在XML创造resources.groovy相当于一个bean这样的:因为它的父如何使用抽象父bean在resources.groovy中创建一个bean?

<bean id="txProxyTemplate" abstract="true" 
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
    <property name="transactionManager" ref="transactionManager" /> 
    <property name="target" ref="genericHibernateDAO" /> 
    <property name="transactionAttributes"> 
     <props> 
      <prop key="save*">PROPAGATION_REQUIRED</prop> 
      <!--prop key="analyze">PROPAGATION_REQUIRED</prop --> 
      <prop key="validate">PROPAGATION_REQUIRED</prop> 
      <prop key="remove*">PROPAGATION_REQUIRED</prop> 
      <prop key="create*">PROPAGATION_REQUIRED</prop> 
      <prop key="delete*">PROPAGATION_REQUIRED</prop> 
      <prop key="add*">PROPAGATION_REQUIRED</prop> 
      <prop key="clear*">PROPAGATION_REQUIRED</prop> 
      <prop key="set*">PROPAGATION_REQUIRED</prop> 
      <prop key="reinitialize">PROPAGATION_REQUIRED</prop> 
      <prop key="zap*">PROPAGATION_REQUIRED</prop> 
      <prop key="turn*">PROPAGATION_REQUIRED</prop> 
      <prop key="*">PROPAGATION_REQUIRED</prop> 
     </props> 
    </property> 
</bean> 

然后使用这个抽象bean第二豆:

<bean id="myCoolService" parent="txProxyTemplate"> 
    <property name="target"> 
     <bean 
      class="com.fourgablesguy.myapp.MyCoolService"> 
     </bean> 
    </property> 
</bean> 

到目前为止,这是我在resources.groovy:

import org.springframework.transaction.interceptor.TransactionProxyFactoryBean 
import com.fourgablesguy.myapp.MyCoolService 

beans = { 
    txProxyTemplate(TransactionProxyFactoryBean) { 
     transactionManager = ref('transactionManager') 
     target = ref ('genericHibernateDAO') 
     transactionAttributes = [ 
      "save*":"PROPAGATION_REQUIRED", 
      "validate":"PROPAGATION_REQUIRED", 
      "remove*":"PROPAGATION_REQUIRED", 
      "create*":"PROPAGATION_REQUIRED", 
      "delete*":"PROPAGATION_REQUIRED", 
      "add*":"PROPAGATION_REQUIRED", 
      "clear*":"PROPAGATION_REQUIRED", 
      "set*":"PROPAGATION_REQUIRED", 
      "reinitialize":"PROPAGATION_REQUIRED", 
      "zap*":"PROPAGATION_REQUIRED", 
      "turn*":"PROPAGATION_REQUIRED", 
      "*":"PROPAGATION_REQUIRED" 
     ] 
    } 

    myCoolService(MyCoolService) { 

    } 
} 

只是不知道如何设置txProxyTemplate bean是抽象和myCoolService豆有txProxyT emplate bean作为父项,myCoolService bean作为目标。

回答

3

这在文档部分中描述,请参阅http://grails.org/doc/latest/guide/spring.html

一般来说,你做一个bean抽象省略它的类,但在这种情况下,由于要指定类,但只把它作为父母豆你需要明确设置abstract属性。为了让其他的bean使用它作为其母公司,设置parent属性:

import org.springframework.transaction.interceptor.TransactionProxyFactoryBean 
import com.fourgablesguy.myapp.MyCoolService 

beans = { 

    txProxyTemplate(TransactionProxyFactoryBean) { bean -> 
     bean.abstract = true 

     transactionManager = ref('transactionManager') 
     target = ref('genericHibernateDAO') 
     transactionAttributes = [ 
     "save*":"PROPAGATION_REQUIRED", 
     "validate":"PROPAGATION_REQUIRED", 
     "remove*":"PROPAGATION_REQUIRED", 
     "create*":"PROPAGATION_REQUIRED", 
     "delete*":"PROPAGATION_REQUIRED", 
     "add*":"PROPAGATION_REQUIRED", 
     "clear*":"PROPAGATION_REQUIRED", 
     "set*":"PROPAGATION_REQUIRED", 
     "reinitialize":"PROPAGATION_REQUIRED", 
     "zap*":"PROPAGATION_REQUIRED", 
     "turn*":"PROPAGATION_REQUIRED", 
     "*":"PROPAGATION_REQUIRED" 
     ] 
    } 

    myCoolService(MyCoolService) { bean -> 
     bean.parent = ref('txProxyTemplate') 
    } 
} 

编辑:重新阅读你的bean DEFS后,看起来这是你真正需要的:

myCoolService { bean -> 
    bean.parent = ref('txProxyTemplate') 
    target = { MyCoolService s -> 
     // props for the inner bean 
    } 
} 
+0

谢谢我发现这些文档,这是正确的。 – fourgablesguy 2012-07-12 18:38:29

+0

我更新了答案,应该更接近你所需要的 – 2012-07-12 18:41:56

相关问题