grails - 如何在 resources.groovy 中使用抽象父 bean 创建 bean?

标签 grails

尝试在 resources.groovy 中创建一个与 XML 中的等效的 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 作为它的父代:

<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 bean 设置为将 txProxyTemplate bean 作为父级,将 myCoolService bean 作为目标。

最佳答案

文档中对此进行了部分描述,请参阅 http://grails.org/doc/latest/guide/spring.html

通常,您通过省略其类来创建 bean 抽象,但在这种情况下,由于您想要指定该类但仅将其用作父 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
   }
}

关于grails - 如何在 resources.groovy 中使用抽象父 bean 创建 bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11457927/

相关文章:

javascript - Grails 渲染内容动态访问 id 元素

java - Grails json 使用列表

grails - 我想提供一个链接以返回 404 上的上一页

grails - 在Grails中映射旧数据库表时避免表更改?

java - 几天后 Grails 和 Jasig CAS 重定向循环

spring - Grails GORM中的树状结构

grails - 是否可以在AWS Lambda上运行Groovy Grails应用程序

grails - Spring 社交ProviderSignInController signupurl

grails - 如何从grails Controller 触发gradle构建?

email - 如何使用groovy模板在Jenkins中发送电子邮件?