1.Spring的AOP基于AspectJ注解开发
(1)Spring的基于ApsectJ的注解的AOP开发
(2)Spring的注解的AOP的通知类型
(3)Spring的注解的AOP的切入点的配置
2.Spring的JDBC的模板的使用
(1)Spring的JDBC的模板
(2)将连接池和模板交给Spring管理
(3)使用开源的数据库连接池
(4)抽取配置到属性文件
(5)使用JDBC的模板完成CRUD的操作
3.Spring事务管理
(1)事务的回顾
(2)Spring的事务管理的API
(3)Spring的事务的传播行为
(4)Spring的事务管理
(5)Spring的事务管理:一类:编程式事务(需要手动编写代码)
(6)Spring的事务管理:二类:声明式事务管理(通过配置实现)---AOP
1.Spring的AOP基于AspectJ注解开发
(1)Spring的基于ApsectJ的注解的AOP开发
第一步:创建项目,引入jar包
6个基础包和4个aop的架包
第二步:编写目标类并配置
第三步:编写切面类并配置
完整的配置文件如下:
第四步:使用注解的AOP对象目标类进行增强
这里的value里面的值,指的是我们需要应用在目标类的哪个方法中
第五步:编写测试类
(2)Spring的注解的AOP的通知类型
1)@Before :前置通知
2)@AfterReturning :后置通知
这里的returning表示的是如果目标类 的方法中有返回值,那么需要得到返回值的话就要加一个returning,注意这里的话returning后面的result要与我们这里传入的参数名称相同。
3)@Around :环绕通知
4)@AfterThrowing :异常抛出通知
5)@After :最终通知(无论是否有错误,最终通知都会执行)
(3)Spring的注解的AOP的切入点的配置
这里我们可以使用切入点注解,方便以后修改
2.Spring的JDBC的模板的使用
(1)Spring的JDBC的模板
Spring是EE开发的一站式的框架,有EE开发的每层的解决方案。Spring对持久层也提供了解决方案:ORM模块和JDBC的模板。Spring提供了很多的模板用于简化开发:
使用入门:
第一步:创建项目,引入jar包
第二步:创建数据库和表
create database spring4_day03;
use spring4_day03;
create table account(
id int primary key auto_increment,
name varchar(20),
money double
);
第三步:使用JDBC模板:保存数据
(2)将连接池和模板交给Spring管理
第一步:引入Spring的配置文件
第二步:使用jdbc模板
引入spring_aop的jar包(在Spring4版本和之后的版本都需要引入,不然会报错)
注意:这里配置Spring的JDBC模板可以直接使用ref是因为方法本身内部就存在dataSource的set方法,所以可以直接使用
(3)使用开源的数据库连接池
DBCP的使用
C3P0的使用
引入c3p0连接池jar包
配置c3p0连接池
(4)抽取配置到属性文件
第一步:定义一个属性文件
第二步:在Spring的配置文件中引入属性文件(两种方式)
1第一种(比较少用)
.
2.第二种(常用)
第三步:引入属性文件的值
第二种方式总的写法:
第四步:编写测试类
(5)使用JDBC的模板完成CRUD的操作
1)保存操作
2)修改操作
3)删除操作
4)查询操作
1.查询某个属性
2.查询返回对象或集合
封装数据的方法
3.Spring事务管理
(1)事务的回顾
事务:逻辑上的一组操作,组成这组操作的各个单元,要么全都成功,要么全都失败
事务的特性:
- 原子性:事务不可分割
- 一致性:事务执行前后数据完整性保持一致
- 隔离性:一个事务的执行不应该受到其他事务的干扰
- 持久性:一旦事务结束,数据就持久化到数据库
如果不考虑隔离性引发安全性问题
- 读问题
- 脏读 :一个事务读到另一个事务未提交的数据
- 不可重复读 :一个事务读到另一个事务已经提交的update的数据,导致一个事务中多次查询结果不一致
- 虚读、幻读 :一个事务读到另一个事务已经提交的insert的数据,导致一个事务中多次查询结果不一致。
- 写问题
- 丢失更新
解决读的问题:
- 设置事务的隔离级别
- Read uncommitted :未提交读,任何读问题解决不了。
- Read committed :已提交读,解决脏读,但是不可重复读和虚读有可能发生。(oracle默认)
- Repeatable read :重复读,解决脏读和不可重复读,但是虚读有可能发生。(mysql默认)
- Serializable :解决所有读问题。
(2)Spring的事务管理的API
1)PlatformTransactionManager:平台事务管理器
- 平台事务管理器:接口,是Spring用于管理事务的真正的对象。
- DataSourceTransactionManager :底层使用JDBC管理事务
- HibernateTransactionManager :底层使用Hibernate管理事务
2)TransactionDefinition :事务定义信息
事务定义:用于定义事务的相关的信息,隔离级别、超时信息、传播行为、是否只读
3)TransactionStatus:事务的状态
事务状态:用于记录在事务管理过程中,事务的状态的对象。
4)事务管理的API的关系
Spring进行事务管理的时候,首先平台事务管理器根据事务定义信息进行事务的管理,在事务管理过程中,产生各种状态,将这些状态的信息记录到事务状态的对象中。
(3)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的事务管理
1)搭建Spring的事务管理的环境
这里的操作是没有使用事务的操作,在转账的时候可能会出现问题,出现的问题就是可以你给其中一个人转账,你钱已经转出去了,这时出现了异常,收钱的人没有收到钱,但是已经保存到数据库中了
第一步:创建Service的接口和实现类
第二步:创建DAO的接口和实现类
第三步:配置Service和DAO:交给Spring管理
第四步:配置连接池和JDBC的模板
第五步:在dao中注入jdbc模板
第六步:测试
(5)Spring的事务管理:一类:编程式事务(需要手动编写代码)(不常用)
第一步:配置平台事务管理器
第二步:Spring提供了事务管理的模板类
第三步:在业务层注入事务管理的模板
第四步:编写事务管理代码
第五步:测试
(6)Spring的事务管理:二类:声明式事务管理(通过配置实现)---AOP
XML方式的声明式事务管理
第一步:引入aop的开发包
第二步:恢复转账环境
第三步:配置事务管理器
第四步:配置增强
name表示在方法名为什么中使用,例如save*表示的就是名字是save开头的, propagation表示的是传播行为,isolation表示隔离方式
第五步:配置aop
整个xml配置如下图
<?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">
<!-- 配置Service============= -->
<bean id="accountService" class="com.itheima.tx.demo2.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置DAO================= -->
<bean id="accountDao" class="com.itheima.tx.demo2.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置连接池和JDBC的模板 -->
<!-- 第二种方式通过context标签引入的 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0连接池=============================== -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置事务管理器=============================== -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务的增强=============================== -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 事务管理的规则 -->
<!-- <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/> -->
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- aop的配置 -->
<aop:config>
<aop:pointcut expression="execution(* com.itheima.tx.demo2.AccountServiceImpl.*(..))" id="pointcut1"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
</aop:config>
</beans>
第六步: 编写测试类
注解方式的声明式事务管理
第一步:引入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">
<!-- 配置Service============= -->
<bean id="accountService" class="com.itheima.tx.demo3.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置DAO================= -->
<bean id="accountDao" class="com.itheima.tx.demo3.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置连接池和JDBC的模板 -->
<!-- 第二种方式通过context标签引入的 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0连接池=============================== -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置事务管理器=============================== -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解事务================================ -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
第五步:在业务层添加注解
第六步:编写测试类