`

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

阅读更多

部分重要代码:

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans 
 	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  	http://www.springframework.org/schema/tx
 	http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
 	http://www.springframework.org/schema/aop 
 	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
 ">
 
 
  <!-- 采用的annotation的方式进行AOP和 IOC -->
    
	<!-- 配置了component-scan可以移除 -->
	 <context:annotation-config /> 
	<!-- 启动对@AspectJ注解的支持 -->
	<aop:aspectj-autoproxy /> 
	<!-- 自动扫描包(自动注入) -->
	<context:component-scan base-package="yingjun" /> 
 
 <!-- 配置数据源 -->
 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath:jdbc.properties"/>
</bean>
 
 <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   <property name="driverClassName" value="${jdbc.driverClassName}"/>
   <property name="url" value="${jdbc.url}"/>
   <property name="username" value="${jdbc.username}"/>
   <property name="password" value="${jdbc.password}"/>
 </bean>
 
<!-- Hibernate配置 -->
  <bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
    <property name="dataSource" ref="DataSource"/>
    <property name="packagesToScan">
      <list>
        <value>yingjun.model</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.MySQLDialect
        hibernate.show_sql=true
      </value>
    </property>
  </bean>
 

   
 <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="SessionFactory"/>
  </bean>
  <!-- 声明试事务管理 -采用annotation方式-->
 <tx:annotation-driven transaction-manager="txManager"/>
 
  <!-- 声明式事务管理 -采用XML方式-->
    <!--常见几张几种事务传播行为类型
       PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。
       PROPAGATION_SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。
       PROPAGATION_MANDATORY 使用当前的事务,如果当前没有事务,就抛出异常。
       PROPAGATION_REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。
    -->

  <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
      <tx:method name="DoAdd*" propagation="REQUIRED"/>
       <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/>
      <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
    </tx:attributes>
  </tx:advice>
  
   <aop:config>
    <aop:pointcut id="productServiceMethods"
                expression="execution(* yingjun.service.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
  </aop:config>
</beans>

 

package yingjun.service;


import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import yingjun.dao.LogDaoI;
import yingjun.dao.UserDaoI;
import yingjun.model.Log;
import yingjun.model.User;


@Component("userService")
public class UserService {
	
	private UserDaoI userdao;
	private LogDaoI logdao;
	
	//@Transactional()
	public void DoAddUser(User user,Log log){
		userdao.AddUser(user);
		logdao.addLog(log);
	
	}
	public UserDaoI getUserdao() {
		return userdao;
	}
	
	@Resource(name="userDao")
	public void setUserdao(UserDaoI userdao) {
		this.userdao = userdao;
	}


	public LogDaoI getLogdao() {
		return logdao;
	}

	@Resource(name="logDao")
	public void setLogdao(LogDaoI logdao) {
		this.logdao = logdao;
	}
	

}

 

package yingjun.test;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import yingjun.model.Log;
import yingjun.model.User;
import yingjun.service.UserService;


public class SpringHibernateTest {
	
	
	@Test
	public void test(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
		UserService us=(UserService)ac.getBean("userService");
		User user=new User();
		Log log=new Log();
		us.DoAddUser(user,log);
	}
	
	
}

 

分享到:
评论

相关推荐

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (1)

    12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1 系统概述 13.2 需求分析 13.2.1 系统用例图 ...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (3)

    12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1 系统概述 13.2 需求分析 13.2.1 系统用例图 ...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (2)

    12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1 系统概述 13.2 需求分析 13.2.1 系统用例图 ...

    spring.doc

    5.1.8声明式事务管理 116 5.1.8.1Spring的事务管理器 117 5.1.8.2Spring事务的传播属性 117 5.1.8.3Spring事务的隔离级别 117 拓展: 118 5.1.8.4以XML配置的 形式 119 拓展: 120 5.1.8.5以注解方式配置 125 拓展:...

    spring in action英文版

     5.3 声明式事务  5.3.1 理解事务属性  5.3.2 声明一个简单的事务策略  5.4 通过方法名声明事务  5.4.1 使用NameMatchTransactionAttributeSource  5.4.2 名称匹配事务的捷径  5.5 用元数据声明...

    Spring-Reference_zh_CN(Spring中文参考手册)

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.6.1. @Transactional 有关的设置 ...

    Spring in Action(第2版)中文版

    6.4声明式事务 6.4.1定义事务参数 6.4.2代理事务 6.4.3在spring2.0里声明事务 6.4.4定义注释驱动事务 6.5小结 第7章保护spring 7.1springsecurity介绍 7.2验证用户身份 7.2.1配置providermanager 7.2.2...

    Spring in Action(第二版 中文高清版).part2

    6.4 声明式事务 6.4.1 定义事务参数 6.4.2 代理事务 6.4.3 在Spring 2.0里声明事务 6.4.4 定义注释驱动事务 6.5 小结 第7章 保护Spring 7.1 Spring Security介绍 7.2 验证用户身份 7.2.1 配置Provider ...

    Spring in Action(第二版 中文高清版).part1

    6.4 声明式事务 6.4.1 定义事务参数 6.4.2 代理事务 6.4.3 在Spring 2.0里声明事务 6.4.4 定义注释驱动事务 6.5 小结 第7章 保护Spring 7.1 Spring Security介绍 7.2 验证用户身份 7.2.1 配置Provider ...

    Spring.3.x企业应用开发实战(完整版).part2

    10.5.2 Hibernate+Spring JDBC混合框架的事务管理 10.6 特殊方法成漏网之鱼 10.6.1 哪些方法不能实施Spring AOP事务 10.6.2 事务增强遗漏实例 10.7 数据连接泄漏 10.7.1 底层连接资源的访问问题 10.7.2 Spring JDBC...

    spring chm文档

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. ...

    Spring中文帮助文档

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知...

    Spring 2.0 开发参考手册

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. ...

    Spring API

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知...

    Spring3.x企业应用开发实战(完整版) part1

    10.5.2 Hibernate+Spring JDBC混合框架的事务管理 10.6 特殊方法成漏网之鱼 10.6.1 哪些方法不能实施Spring AOP事务 10.6.2 事务增强遗漏实例 10.7 数据连接泄漏 10.7.1 底层连接资源的访问问题 10.7.2 Spring JDBC...

    Java Web程序设计教程

    13.3.3声明式事务处理 267 13.3.4标注式事务处理 268 13.4项目实战——公司人事管理 269 本章小结 276 课后练习 276 第14章spring与struts2、hibernate框架的整合基础 277 14.1spring与struts2的整合方式 277...

    Spring攻略(第二版 中文高清版).part1

    3.2 用AspectJ注解声明aspect 115 3.2.1 问题 115 3.2.2 解决方案 115 3.2.3 工作原理 116 3.3 访问连接点信息 121 3.3.1 问题 121 3.3.2 解决方案 122 3.3.3 工作原理 122 3.4 指定aspect优先级 ...

    低清版 大型门户网站是这样炼成的.pdf

    6.3.1 spring 2.5的声明式事务管理 394 6.3.2 基于xml方式的事务管理配置 396 6.3.3 基于annotation方式的事务管理配置 400 6.4 “桃园三结义”——ssh 2组合开发框架始成 404 6.4.1 spring 2.5集成orm中间件...

    Spring攻略(第二版 中文高清版).part2

    3.2 用AspectJ注解声明aspect 115 3.2.1 问题 115 3.2.2 解决方案 115 3.2.3 工作原理 116 3.3 访问连接点信息 121 3.3.1 问题 121 3.3.2 解决方案 122 3.3.3 工作原理 122 3.4 指定aspect优先级 ...

    javaeye热点阅读

    1.13 Excel Report 一个小巧实用基于java实现的Excel报表生成工具 1.14 Spring(十四) Spring和Hibernate的结合--声明式事务管理1.15 Apache Click 框架初探1.16 Spring AOP 详解 1.17 2 Spring2.0用注解实现事务...

Global site tag (gtag.js) - Google Analytics