spring3.2+hibernate4.1采用声明式的事务处理

http://wosyingjun.iteye.com/blog/1839858

部分重要代码:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="  
  6.     http://www.springframework.org/schema/beans   
  7.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.     http://www.springframework.org/schema/tx  
  9.     http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  10.     http://www.springframework.org/schema/aop   
  11.     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  12.     http://www.springframework.org/schema/context     
  13.     http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  14.  ">  
  15.    
  16.    
  17.   <!-- 采用的annotation的方式进行AOP和 IOC -->  
  18.       
  19.     <!-- 配置了component-scan可以移除 -->  
  20.      <context:annotation-config />   
  21.     <!-- 启动对@AspectJ注解的支持 -->  
  22.     <aop:aspectj-autoproxy />   
  23.     <!-- 自动扫描包(自动注入) -->  
  24.     <context:component-scan base-package="yingjun" />   
  25.    
  26.  <!-- 配置数据源 -->  
  27.  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  28.   <property name="locations" value="classpath:jdbc.properties"/>  
  29. </bean>  
  30.    
  31.  <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  32.    <property name="driverClassName" value="${jdbc.driverClassName}"/>  
  33.    <property name="url" value="${jdbc.url}"/>  
  34.    <property name="username" value="${jdbc.username}"/>  
  35.    <property name="password" value="${jdbc.password}"/>  
  36.  </bean>  
  37.    
  38. <!-- Hibernate配置 -->  
  39.   <bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >  
  40.     <property name="dataSource" ref="DataSource"/>  
  41.     <property name="packagesToScan">  
  42.       <list>  
  43.         <value>yingjun.model</value>  
  44.       </list>  
  45.     </property>  
  46.     <property name="hibernateProperties">  
  47.       <value>  
  48.         hibernate.dialect=org.hibernate.dialect.MySQLDialect  
  49.         hibernate.show_sql=true  
  50.       </value>  
  51.     </property>  
  52.   </bean>  
  53.    
  54.   
  55.      
  56.  <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  57.     <property name="sessionFactory" ref="SessionFactory"/>  
  58.   </bean>  
  59.   <!-- 声明试事务管理 -采用annotation方式-->  
  60.  <tx:annotation-driven transaction-manager="txManager"/>  
  61.    
  62.   <!-- 声明式事务管理 -采用XML方式-->  
  63.     <!--常见几张几种事务传播行为类型  
  64.        PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。  
  65.        PROPAGATION_SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。  
  66.        PROPAGATION_MANDATORY 使用当前的事务,如果当前没有事务,就抛出异常。  
  67.        PROPAGATION_REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。  
  68.     -->  
  69.   
  70.   <tx:advice id="txAdvice" transaction-manager="txManager">  
  71.     <tx:attributes>  
  72.       <tx:method name="DoAdd*" propagation="REQUIRED"/>  
  73.        <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/>  
  74.       <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>  
  75.     </tx:attributes>  
  76.   </tx:advice>  
  77.     
  78.    <aop:config>  
  79.     <aop:pointcut id="productServiceMethods"  
  80.                 expression="execution(* yingjun.service.*.*(..))"/>  
  81.     <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>  
  82.   </aop:config>  
  83. </beans>  

 

Java代码   收藏代码
  1. package yingjun.service;  
  2.   
  3.   
  4. import javax.annotation.Resource;  
  5.   
  6. import org.springframework.stereotype.Component;  
  7. import org.springframework.transaction.annotation.Transactional;  
  8.   
  9. import yingjun.dao.LogDaoI;  
  10. import yingjun.dao.UserDaoI;  
  11. import yingjun.model.Log;  
  12. import yingjun.model.User;  
  13.   
  14.   
  15. @Component("userService")  
  16. public class UserService {  
  17.       
  18.     private UserDaoI userdao;  
  19.     private LogDaoI logdao;  
  20.       
  21.     //@Transactional()  
  22.     public void DoAddUser(User user,Log log){  
  23.         userdao.AddUser(user);  
  24.         logdao.addLog(log);  
  25.       
  26.     }  
  27.     public UserDaoI getUserdao() {  
  28.         return userdao;  
  29.     }  
  30.       
  31.     @Resource(name="userDao")  
  32.     public void setUserdao(UserDaoI userdao) {  
  33.         this.userdao = userdao;  
  34.     }  
  35.   
  36.   
  37.     public LogDaoI getLogdao() {  
  38.         return logdao;  
  39.     }  
  40.   
  41.     @Resource(name="logDao")  
  42.     public void setLogdao(LogDaoI logdao) {  
  43.         this.logdao = logdao;  
  44.     }  
  45.       
  46.   
  47. }  

 

Java代码   收藏代码
  1. package yingjun.test;  
  2.   
  3.   
  4. import org.junit.Test;  
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. import yingjun.model.Log;  
  9. import yingjun.model.User;  
  10. import yingjun.service.UserService;  
  11.   
  12.   
  13. public class SpringHibernateTest {  
  14.       
  15.       
  16.     @Test  
  17.     public void test(){  
  18.         ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");  
  19.         UserService us=(UserService)ac.getBean("userService");  
  20.         User user=new User();  
  21.         Log log=new Log();  
  22.         us.DoAddUser(user,log);  
  23.     }  
  24.       
  25.       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值