Spring学习总结(三)

一、Spring的AOP的基于AspectJ注解开发

1、引入jar包

在这里插入图片描述

2、引入配置文件并编写类进行测试

1、编写目标类和切面类
package spring.demo1;
public class OrderDao {

	public void save() {
		System.out.println("保存订单");
	}

	public void update() {
		System.out.println("修改订单");
	}

	public void find() {
		System.out.println("查找订单");
	}

	public void delete() {
		System.out.println("删除订单");
	}

	// 后置方法返回值
	public String deleteReturn() {
		System.out.println("删除订单并返回返回值");
		return "蔡徐坤";
	}
}

package spring.demo1;
import org.aspectj.lang.annotation.Aspect;

/**
 * 切面类:注解方式的切面类
 * 
 * @author Administrator
 *
 */
@Aspect
public class MyAspectAnno {

	@Before(value = "execution(* spring.demo1.OrderDao.save(..))")
	public void before1() {
		System.out.println("前置增强");
	}
}

2、配置目标类和切面类并开启注解的AOP开发
<?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:aop="http://www.springframework.org/schema/aop" 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/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!--在配置文件中开启注解的AOP开发 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	<!--配置目标类 -->
	<bean id="orderDao" class="spring.demo1.OrderDao">
	</bean>
	<!--配置切面类 -->
	<bean id="myAspect" class="spring.demo1.MyAspectAnno">
	</bean>
</beans>

3、编写测试类
package spring.demo1;

import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Spring的AOP的注解开发
 * 
 * @author Administrator
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo1 {
	@Resource(name = "orderDao")
	private OrderDao orderDao;

	@Test
	public void demo1() {
		orderDao.save();
		orderDao.update();
		orderDao.find();
		orderDao.delete();
		orderDao.deleteReturn();
	}
}
这里记得导入spring-test-4.2.4.RELEASE.jar包,整合JUnit4调试的包。

4、测试结果
	前置增强
	保存订单
	修改订单
	查找订单
	删除订单
	删除订单并返回返回值

3、Spring的注解的AOP的通知类型

1、前置通知
	@Before(value = "execution(* spring.demo1.OrderDao.save(..))")
	public void before1() {
		System.out.println("前置增强");
	}
2、后置通知
	@AfterReturning(value = "execution(* spring.demo1.OrderDao.delete(..))")
	public void afterReturning() {
		System.out.println("前置增强");
	}
	
	带返回值
	@AfterReturning(value = "execution(* spring.demo1.OrderDao.delete(..))",returning="result")
	public void afterReturning(Object result) {
		System.out.println("前置增强"+reslut);
	}

3、环绕通知
	@Around(value = "execution(* spring.demo1.OrderDao.update(..))")
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("环绕前增强");
		Object obj = joinPoint.proceed();
		System.out.println("环绕后增强");
		return obj;
	}

4、异常抛出通知
	@AfterThrowing(value = "execution(* spring.demo1.OrderDao.find(..))", throwing = "e")
	public void afterThrowing(Throwable e) {
		System.out.println("异常抛出增强" + e.getMessage());
	}	

5、最终通知
	@After(value = "execution(* spring.demo1.OrderDao.find(..))")
	public void after() {
		System.out.println("最终增强");
	}

4、Spring的注解的AOP的切入点的配置

// 切入点注解
	@Pointcut(value = "execution(* spring.demo1.OrderDao.save(..))")
	private void pointcut1() {
	}
	@Pointcut(value = "execution(* spring.demo1.OrderDao.update(..))")
	private void pointcut2() {
	}
	@Pointcut(value = "execution(* spring.demo1.OrderDao.find(..))")
	private void pointcut3() {
	}
	@Pointcut(value = "execution(* spring.demo1.OrderDao.delete(..))")
	private void pointcut4() {
	}
	@Pointcut(value = "execution(* spring.demo1.OrderDao.deleteReturn(..))")
	private void pointcut5() {
	}

//使用
	// 前置通知
	@Before(value = "MyAspectAnno.pointcut1()")
	public void before() {
		System.out.println("前置增强");
	}

	// 后置通知
	@AfterReturning(value = "MyAspectAnno.pointcut4()")
	public void afterReturning() {
		System.out.println("后置增强");
	}

	// 后置通知
	@AfterReturning(value = "MyAspectAnno.pointcut5()", returning = "result")
	public void afterReturningReturn(Object result) {
		System.out.println("后置增强" + result);
	}

	// 环绕通知
	@Around(value = "MyAspectAnno.pointcut2()")
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("环绕前增强");
		Object obj = joinPoint.proceed();
		System.out.println("环绕后增强");
		return obj;
	}

	// 异常抛出通知
	@AfterThrowing(value = "MyAspectAnno.pointcut3()", throwing = "e")
	public void afterThrowing(Throwable e) {
		System.out.println("异常抛出增强" + e.getMessage());
	}

	// 最终通知
	@After(value = "MyAspectAnno.pointcut4()")
	public void after() {
		System.out.println("最终增强");
	}

二、Spring的JDBC的模板的使用

1、Spring的JDBC的模板

Spring是EE开发的一站式的框架,有EE开发的每层的解决方案。Spring对持久层也提供了解决方案:ORM模块和JDBC的模板。Spring提供了很多的模板用于简化开发:
在这里插入图片描述

JDBC模板使用的入门

1、创建项目,引入jar包
* 引入基本开发包:
* 数据库驱动
* Spring的JDBC模板的jar包
在这里插入图片描述
2、创建数据库和表

create database spring4_day03;
use spring4_day03;
create table account(
	id int primary key auto_increment,
	name varchar(20),
	money double
);

3、使用JDBC的模板:保存数据

package jdbc.demo1;

import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * JDBC模板的使用
 * 
 * @author Administrator
 *
 */
public class JdbcDemo1 {
	@Test
	public void demo1() {
		// 创建连接池
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost/spring4_day03");
		dataSource.setUsername("root");
		dataSource.setPassword("123456");
		// 创建jdbc模板
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

		jdbcTemplate.update("insert into account values(null,?,?)", "吴彦祖", 10000d);
	}
}

2、将连接池和模板交给Spring管理

1、引入Spring配置文件

	<!--配置Spring的内置连接池========== -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
		<!--属性注入--> 
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
		<property name="url" value="jdbc:mysql://localhost:3306/spring4_day03"></property> 
		<property name="username" value="root"></property>
		<property name="password" value="123456"></property>
	 </bean> 
	<!--配置Spring的JDBC的模板 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

2、使用Jdbc的模板

package jdbc.demo1;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import jdbc.domain.Account;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcDemo2 {
	@Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;

	@Test
	// 保存操作
	public void demo2() {
		jdbcTemplate.update("insert into account values(null,?,?)", "梁家辉", 10000d);
	}
}

3、使用开源的数据库连接池

1、DBCP的使用

  • 引入jar包
    在这里插入图片描述
    2、配置DBCP连接池
<!--配置DBCP连接池 -->
	 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
		<!--属性注入--> 
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
		<property name="url" value="jdbc:mysql://localhost:3306/spring4_day03"></property> 
		<property name="username" value="root"></property> 
		<property name="password" value="123456"></property> 
	</bean>
	
	<!--配置Spring的JDBC的模板 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>	

3、C3P0的使用
引入C3P0连接池jar包
在这里插入图片描述

	<!--配置C3P0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
		<!--属性注入(name与前面两个连接池不一样了)-->
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property> 
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring4_day03"></property> 
		<property name="user" value="root"></property> 
		<property name="password" 	value="123456"></property>
	 </bean>
	
	<!--配置Spring的JDBC的模板 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>	

4、抽取配置到属性文件

1、定义一个属性文件

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring4_day03
jdbc.username=root
jdbc.password=123456

2、在Spring的配置文件中引入属性文件

	<!--引入属性文件 -->
	<!--第一种方式通过一个bean标签引入的(很少) -->
	 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:jdbc.properties"></property>
	</bean>

	<!--第二种方式通过context标签引入的 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>

3、引入属性文件的值

	<!--配置C3P0连接池========-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
		<!-- 属性注入(name与前面两个连接池不一样了)  -->
		<property name="driverClass" value="${jdbc.driverClass}"></property> 
		<property name="jdbcUrl" value="${jdbc.url}"></property> 
		<property name="user" value="${jdbc.username}"></property> 
		<property name="password" 	value="${jdbc.password}"></property>
	</bean>

4、测试

	@Test
	// 保存操作
	public void demo2() {
		jdbcTemplate.update("insert into account values(null,?,?)", "梁家辉", 10000d);
	}

5、使用JDBC的模板完成CRUD的操作

1、保存操作

	@Test
	// 保存操作
	public void demo2() {
		jdbcTemplate.update("insert into account values(null,?,?)", "梁家辉", 10000d);
	}

2、修改操作

	@Test
	// 修改操作
	public void demo3() {
		jdbcTemplate.update("update account set name=?,money=? where id=?", "周润发", 10000d, 3);
	}

3、删除操作

	@Test
	// 删除操作
	public void demo4() {
		jdbcTemplate.update("delete from account where id=?", 7);
	}

4、查询操作

  • 查询某个属性
	@Test
	// 查询操作
	public void demo5() {
		String name = jdbcTemplate.queryForObject("select name from account where id=?", String.class, 5);
		System.out.println(name);
	}
	@Test
	// 统计查询
	public void demo6() {
		Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
		System.out.println(count);
	}
  • 查询返回对象或集合
	@Test
	// 封装到一个对象中
	public void demo7() {
		Account account = jdbcTemplate.queryForObject("select * from account where id=?", new MyRowMapper(), 5);
		System.out.println(account);
	}
	@Test
	// 查询多条记录
	public void demo8() {
		List<Account> list = jdbcTemplate.query("select * from account", new MyRowMapper());
		for (Account account : list) {
			System.out.println(account);
		}
}
  • 数据封装
class MyRowMapper implements RowMapper<Account> {
		@Override
		public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
			Account account = new Account();
			account.setId(rs.getInt("id"));
			account.setName(rs.getString("name"));
			account.setMoney(rs.getDouble("money"));
			return account;
		}
	}
  • Account实体类
package jdbc.domain;
public class Account {
	private Integer id;
	private String name;
	private Double money;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getMoney() {
		return money;
	}
	public void setMoney(Double money) {
		this.money = money;
	}
	@Override
	public String toString() {
		return "Account [id=" + id + ", name=" + name + ", money=" + money + "]";
	}	
}

三、Spring的事务管理

1、事务的回顾

1.1、什么是事务

事务:逻辑上的一组操作,组成这组操作的各个单元,要么全都成功,要么全都失败。

1.2、事务的特性

  • 原子性:事务不可分割
  • 一致性:事务执行前后数据完整性保持一致
  • 隔离性:一个事务的执行不应该受到其他事务的干扰
  • 持久性:一旦事务结束,数据就持久化到数据库

1.3、如果不考虑隔离性引发安全性问题

读问题

  • 脏读 :一个事务读到另一个事务未提交的数据
  • 不可重复读 :一个事务读到另一个事务已经提交的update的数据,导致一个事务中多次查询结果不一致
  • 虚读、幻读 :一个事务读到另一个事务已经提交的insert的数据,导致一个事务中多次查询结果不一致。

写问题

  • 丢失更新

1.4、解决读问题

设置事务的隔离级别

  • Read uncommitted :未提交读,任何读问题解决不了。
  • Read committed :已提交读,解决脏读,但是不可重复读和虚读有可能发生。
  • Repeatable read :重复读,解决脏读和不可重复读,但是虚读有可能发生。
  • Serializable :解决所有读问题。

2、Spring的事务管理的API

2.1、PlatformTransactionManager:平台事务管理器

平台事务管理器:接口,是Spring用于管理事务的真正的对象。

  • DataSourceTransactionManager :底层使用JDBC管理事务
  • HibernateTransactionManager :底层使用Hibernate管理事务

2.2、TransactionDefinition :事务定义信息

事务定义:用于定义事务的相关的信息,隔离级别、超时信息、传播行为、是否只读

2.3、TransactionStatus:事务的状态

2.4、事务管理的API的关系

Spring进行事务管理的时候,首先平台事务管理器根据事务定义信息进行事务的管理,在事务管理过程中,产生各种状态,将这些状态的信息记录到事务状态的对象中。

3、Spring的事务的传播行为

3.1、Spring的传播行为

Spring中提供了七种事务的传播行为:

保证多个操作在同一个事务中

  • PROPAGATION_REQUIRED :默认值,如果A中有事务,使用A中的事务,如果A没有,创建一个新的事务,将操作包含进来(重要)
  • PROPAGATION_SUPPORTS :支持事务,如果A中有事务,使用A中的事务。如果A没有事务,不使用事务。
  • PROPAGATION_MANDATORY :如果A中有事务,使用A中的事务。如果A没有事务,抛出异常。

保证多个操作不在同一个事务中

  • PROPAGATION_REQUIRES_NEW :如果A中有事务,将A的事务挂起(暂停),创建新事务,只包含自身操作。如果A中没有事务,创建一个新事务,包含自身操作。(重要)
  • PROPAGATION_NOT_SUPPORTED :如果A中有事务,将A的事务挂起。不使用事务管理。
  • PROPAGATION_NEVER :如果A中有事务,报异常。

嵌套式事务

  • PROPAGATION_NESTED :嵌套事务,如果A中有事务,按照A的事务执行,执行完成后,设置一个保存点,执行B中的操作,如果没有异常,执行通过,如果有异常,可以选择回滚到最初始位置,也可以回滚到保存点。(重要)

4、Spring的事务管理

4.1 搭建Spring的事务管理的环境

  • 创建Service的接口和实现类
/**
 * 转账的业务层的实现类
 * 
 * @author Administrator
 *
 */
@Transactional
public class AccountServiceImpl implements AccountService {

	// 注入DAO
	private AccountDao accountDao;

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	
	@Override
	/**
	 * from:转出账号 to:转入账号 money:转账金额
	 */
	public void transfer(final String from, final String to, final Double money) {
		
		accountDao.outMoney(from, money);
		//int d=1/0;
		accountDao.inMoney(to, money);
	}
}
  • 创建DAO的接口和实现类
package jdbc.tx.demo1;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

	@Override
	public void outMoney(String from, Double money) {
		this.getJdbcTemplate().update("update account set money = money - ? where name=?", money, from);
	}

	@Override
	public void inMoney(String to, Double money) {
		this.getJdbcTemplate().update("update account set money = money + ? where name= ?", money, to);
	}
}
  • 配置Service和DAO:交给Spring管理
	<!--配置Service -->
	<bean id="accountService" class="jdbc.tx.demo1.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"></property>
	</bean>

	<!--配置DAO -->
	<bean id="accountDao" class="jdbc.tx.demo1.AccountDaoImpl">
	
	</bean>
  • 在DAO中编写扣钱和加钱的方法:
    配置连接池和JDBC的模板
<!--配置连接池和JDBC模板 -->
	<!--第二种方式通过context标签引入的 -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 属性注入(name与前面两个连接池不一样了) -->
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

在DAO注入JDBC的模板

<!--配置DAO -->
	<bean id="accountDao" class="jdbc.tx.demo1.AccountDaoImpl">

		<!--第一种方式直接调用jdbcTemplate,但是需要配置Spring的JDBC的模板 -->
		 <property name="jdbcTemplate" ref="jdbcTemplate"></property>

		<!--第二种方式,可以调用dataSource,不需要配置Spring的JDBC的模板,因为继承的JdbcDaoSupport
					的方法 如果提供dataSource的话就可以自动创建jdbcTemplate -->
		<!-- <property name="dataSource" ref="dataSource"></property> -->
	</bean>

	<!--配置Spring的JDBC的模板 -->(可省略这个配置通过第二种方式)
	 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean> 

测试

package jdbc.tx.demo1;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 测试转账的环境
 * 
 * @author Administrator
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:tx.xml")
public class SpringDemo1 {
	
	@Resource(name = "accountService")
	private AccountService accountService;

	@Test
	public void demo1() {
		accountService.transfer("吴彦祖", "古天乐", 2000d);
	}
}

总结:如果出现转账中的异常的话,可能会出现这样的情况,A转账成功,钱已经扣了,但是在B收到的这个过程中出现异常,导致B没有收到钱,这个时候就需要事务了。

5、Spring的事务管理:一类:编程式事务(需要手动编写代码)–了解

5.1 第一步:配置平台事务管理器

<!--配置平台事务管理器  -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

5.2 第二步:Spring提供了事务管理的模板类

<!--配置事务管理的模板  -->
	<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
		<property name="transactionManager" ref="transactionManager"></property>
	</bean>

5.3 第三步:在业务层注入事务管理的模板

	<!--配置Service -->
	<bean id="accountService" class="jdbc.tx.demo1.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"></property>
		<!--注入事务管理的模板  -->
		<property name="transactionTemplate" ref="transactionTemplate"></property>
	</bean>

5.4 编写事务管理的代码

@Override
	/**
	 * from:转出账号 to:转入账号 money:转账金额
	 */
	public void transfer(final String from, final String to, final Double money) {
		
		transactionTemplate.execute(new TransactionCallbackWithoutResult(){
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
				accountDao.outMoney(from, money);
				int d=1/0;
				accountDao.inMoney(to, money);
			}		
		});
	}

5.5 测试

package jdbc.tx.demo1;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 测试转账的环境
 * 
 * @author Administrator
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:tx.xml")
public class SpringDemo1 {
	
	@Resource(name = "accountService")
	private AccountService accountService;

	@Test
	public void demo1() {
		accountService.transfer("吴彦祖", "古天乐", 2000d);
	}
}

6、Spring的事务管理:二类:声明式事务管理(通过配置实现)—AOP

6.1 XML方式的声明式事务管理

第一步:引入aop的开发包
第二步:恢复转账环境
第三步:配置事务管理器

<!--配置平台事务管理器  -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

第四步:配置增强

<!--配置增强  -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!--事务管理的规则  -->
			<!-- <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"></tx:method>
			<tx:method name="upadate*" propagation="REQUIRED"></tx:method>
			<tx:method name="delete*" propagation="REQUIRED"></tx:method>
			<tx:method name="find*" read-only=""></tx:method> -->
			<tx:method name="*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice> 

第五步:AOP的配置

<!--AOP的配置  -->
	<aop:config>
		<aop:pointcut expression="execution(* jdbc.tx.demo3.AccountServiceImpl.*(..))" id="pointcut1"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
	</aop:config>

第六步:测试(略)

6.2 注解方式的声明式事务管理

第一步:引入aop的开发包
第二步:恢复转账环境
第三步:配置事务管理器

	<!--配置平台事务管理器  -->
 	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

第四步:开启注解事务

	<!--开启注解事务  -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

第五步:在业务层添加注解

package jdbc.tx.demo2;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * 转账的业务层的实现类
 * 
 * @author Administrator
 *
 */
@Transactional
public class AccountServiceImpl implements AccountService {

	// 注入DAO
	private AccountDao accountDao;

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	
	//注入事务管理的模板
/*	private TransactionTemplate transactionTemplate;

	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}
*/
	@Override
	/**
	 * from:转出账号 to:转入账号 money:转账金额
	 */
	public void transfer(final String from, final String to, final Double money) {
		
		accountDao.outMoney(from, money);
		int d=1/0;
		accountDao.inMoney(to, money);
	}
}

第六步:测试(略)

注:Spring的基础知识基本上已经介绍完了,其中不明白的地方还是得多看看其源码,通过查看源码可以详细知道它的配置方法,而且也可以省去很多多余的代码,接下来就要进入SSH三大框架的整合,通过Spring管理Struts2和Hibernate的XML配置,希望大家可以从这篇文章或者接下来的文章得到自己想要的,总结不易,点个赞吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值