2012-07-12 64 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

相關問題