fourgablesguy
fourgablesguy

Reputation: 469

How do I create a bean in resources.groovy with an abstract parent bean?

Trying to create a bean in resources.groovy equivalent to this in XML:

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

Then a second bean which uses this abstract bean as it's parent:

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

So far this is what I have in 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) {

    }
}

Just not sure how to setup the txProxyTemplate bean to be abstract and the myCoolService bean to have the txProxyTemplate bean as the parent and the myCoolService bean as the target.

Upvotes: 2

Views: 3696

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75671

This is partially described in the docs, see http://grails.org/doc/latest/guide/spring.html

In general you make a bean abstract by omitting its class, but in this case since you want to specify the class but only use it as a parent bean you need to set the abstract property explicitly. To make the other bean use it as its parent, set the parent property:

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')
   }
}

EDIT: After re-reading your bean defs, it looks like this is what you really need:

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

Upvotes: 3

Related Questions