论坛首页 Java企业应用论坛

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

浏览 5681 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-04-01  

部分重要代码:

<?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);
	}
	
	
}

 

   发表时间:2013-04-02  
楼主想表达什么?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics