Spring之使用事务缘由(2-注解实现)

 

Spring事务注解实现

 

1. 依赖包:

    1.1 spring包:

          spring-beans-4.0.0.RELEASE.jar

          spring-context-4.0.0.RELEASE.jar

          spring-core-4.0.0.RELEASE.jar

          spring-expression-4.0.0.RELEASE.jar

          spring-aop-4.0.0.RELEASE.jar

          spring-jdbc-4.0.0.RELEASE.jar

          spring-tx-4.0.0.RELEASE.jar

 

    1.2 commons包:

          commons-logging-1.1.1.jar

 

    1.3 aspectj包:

          com.springsource.org.aopalliance-1.0.0.jar

          com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

          com.springsource.net.sf.cglib-2.2.0.jar

     

    1.4 数据库驱动包:

          mysql-connector-java-5.1.30-bin.jar

          c3p0-0.9.1.2.jar

 

2. 事务配置步骤:

    2.1 加入依赖包到classpath;

    2.2 在配置文件中加入aop、tx的命名空间;

    2.3 基于注解的方式;

 

Spring 允许简单地用 @Transactional 注解来标注事务方法。 为了将方法定义为支持事务处理的,可以为方法添加 @Transactional 注解。根据 Spring AOP 基于代理机制,只能标注公有方法。可以在方法或者类级别上添加 @Transactional 注解。当把这个注解应用到类上时,这个类中的所有公共方法都会被定义成支持事务处理的。在 Bean 配置文件中只需要启用 <tx:annotation-driven> 元素,并为之指定事务管理器就可以了。如果事务处理器的名称是 transactionManager,就可以在<tx:annotation-driven> 元素中省略 transaction-manager 属性。这个元素会自动检测该名称的事务处理器。

 

<?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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	
	<context:component-scan base-package="xyz.huning.spring4.transaction.annotation"></context:component-scan>
	
	<!-- 导入资源文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置 C3P0 数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>

		<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
		<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
		<property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
		<property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
		<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"></property>
	</bean>
	
	<!-- 配置 NamedParameterJdbcTemplate, 该对象可以使用具名参数, 其没有无参数的构造器, 所以必须为其构造器指定参数 -->
	<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
		<constructor-arg ref="dataSource"></constructor-arg>	
	</bean>
	
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
</beans>

 

 

package xyz.huning.spring4.transaction.annotation.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import xyz.huning.spring4.transaction.dao.ISalaryDao;
import xyz.huning.spring4.transaction.exception.PayoffException;
import xyz.huning.spring4.transaction.model.Account;
import xyz.huning.spring4.transaction.model.Company;
import xyz.huning.spring4.transaction.model.Employee;
import xyz.huning.spring4.transaction.service.ISalaryService;

@Service
public class SalaryService implements ISalaryService{

	@Autowired
	private ISalaryDao salaryDao;
	
	public Employee getEmployee(String name)
	{
		return salaryDao.getEmployee(name);
	}
	
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public Employee payoff(Employee employee)
	{
		Company company = salaryDao.getCompany(ISalaryService.COMPANY_NAME);
		Account comAccount = salaryDao.getAccount(company);
		if(comAccount.getBalance() < employee.getSalary())
		{
		    throw new PayoffException("The company's balance is not enough!");
		}
		salaryDao.updateAccount(company, comAccount.getBalance() - employee.getSalary());
		
		Account empAccount = salaryDao.getAccount(employee);
		if(empAccount.getStatus() != ISalaryService.ACCOUNT_STATUS_AVAILABLE)
		{
			throw new PayoffException("The employee's account is not available!");
		}
		salaryDao.updateAccount(employee, empAccount.getBalance() + employee.getSalary());
		return employee;
	}
	

	@Transactional
	public List<Employee> payoff(List<Employee> employees)
	{
		List<Employee> result = new ArrayList<Employee>();
		for(Employee employee : employees)
		{
			result.add(payoff(employee));
		}
		return result;
	}
	
}

 

 

 

你可能感兴趣的:(spring)