Tuesday, April 10, 2012

Spring: Spring ROM Support for Hibernate with MySQL Part 2

This is an article that extends the previous article Spring: Spring ROM Support for Hibernate with MySQL. I have extended that article by introducing how you can do declarative transaction management for your services. The sample code for this post can be found here.

Everything looks exactly the same as the previous post except for pom.xml and dao.xml Spring configuration file as follows.

In pom.xml I have added some aspectJ dependencies that enables AOP.
  <!--AspectJ dependency-->
  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>${org.aspectj.version}</version>
  </dependency>
  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>${org.aspectj.version}</version>
  </dependency>

In the dao.xml I have defined the beans for declarative transaction management.
 <!-- transaction management -->
 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory"/>
 </bean>
 <tx:advice id="txAdvice" transaction-manager="txManager">
   <tx:attributes>
     <tx:method name="get" read-only="true"/>
     <tx:method name="*"/>
   </tx:attributes>
 </tx:advice>
 <aop:config>
   <aop:pointcut id="serviceOperationsPC"
          expression="execution(* org.fazlan.spring.hibernate.service.IService.*(..))"/>
   <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperationsPC"/>
 </aop:config>

Summary:
This article looked at how you can do declarative transaction management with Spring.

No comments:

Post a Comment