Difference between spring tx:advice and spring aop pointcut

I am new to spring, with working knowledge of hibernate. My job was to implement transaction by using spring declarative approach.And successfully i did with the help of Google, thanks to Google. But not able to understand clearly about the terms I used in application-context.xml.

1.

 <tx-advice>

</tx-advice>
  1. <aop-config>
        // here is point cut were declared
    </aop-config>
    

can somebody explain me about above point, Meanwhile I am trying to understand it from the google also.

Answer

As you already successfully implemented spring transaction,

In Spring we can implement transaction in three ways:

  1. Platform Transaction Management.
  2. Declarative Transaction Management.
  3. Programmatic Transaction Management.

What you implemented is called Declarative Transaction Management via XML.

In short you did the implementation of transaction by Spring’s AOP feature.

Coupling the tx:advice XML configuration with an XML based AOP configuration makes for synergistic combination. For example, we can use method names to automatically figure out what kind of transaction we want to apply on that method.

Say we want to apply the transaction on all that methods which start with save and modify such as savePizza(),saveColdDrink(),modifyOrder(),modifyBill(). For these we have to define the advice in our xml file:

<tx:advice id="txAdvice" >
  <tx:attributes>
    <tx:method name="save*" propagation="REQUIRED"/>
    <tx:method name="modify*" propagation="REQUIRED"/>
  </tx:attributes>
</tx:advice> 

Our advice is ready, as we said by using above line that we want transactions only on the methods which start with save or modify. Now we are going to say which beans require the above advice by using pointcut element of aop-config. For example let say we want to apply the transaction advice to all of the classes which are available inside the com.mytransaction.service package.

For this, we have to add the following line inside our xml file:

<aop:config>
  <aop:pointcut id="allServices"
    expression="execution(*com.mytransaction.service.*.*(..))"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="allServices"/>
</aop:config>

In-short, <tx:advice> mean what to do or which behavior of transaction we want to apply. pointcut element inside <aop-config> says where we want to apply the transaction, say <aop:advisor advice-ref="txAdvice" pointcut-ref="allServices"/>

Leave a Reply

Your email address will not be published. Required fields are marked *