Spring筆記三

1 Spring 使用 AspectJ 進行 AOP 的開發:註解的方式

1.1 引入相關的 jar 包:

* spring 的傳統 AOP 的開發的包
* spring-aop-4.2.4.RELEASE.jar
* com.springsource.org.aopalliance-1.0.0.jar
* aspectJ 的開發包:
* com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

* spring-aspects-4.2.4.RELEASE.jar


1.2 引入 Spring 的配置文件

引入 AOP 約束:
<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" xsi:schemaLocation="
     http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

1.3 編寫目標類:

public class ProductDao {
    public void save(){
    System.out.println("保存商品...");
    }
    public void update(){
    System.out.println("修改商品...");
    }
    public void delete(){
    System.out.println("刪除商品...");
    }
    public void find(){
    System.out.println("查詢商品...");
    }
}

1.4 配置目標類:

 <!-- 目標類============ -->
 <bean id="productDao" class="cn.itcast.spring.demo4.ProductDao"></bean>

1.5 開啓 aop 註解的自動代理:

<aop:aspectj-autoproxy/>

1.6 AspectJ 的 AOP 的註解:

@Aspect:定義切面類的註解
通知類型:
 * @Before :前置通知
 * @AfterReturing :後置通知
 * @Around :環繞通知
 * @After :最終通知
 * @AfterThrowing :異常拋出通知.
 * @Pointcut:定義切入點的註解

1.7 編寫切面類:

@Aspect
public class MyAspectAnno {
    @Before("MyAspectAnno.pointcut1()")
    public void before(){
    System.out.println("前置通知===========");
    }
    @Pointcut("execution(* cn.itcast.spring.demo4.ProductDao.save(..))")

    private void pointcut1(){

    }

}

1.8 配置切面:

 <!-- 配置切面類 -->
 <bean id="myAspectAnno" class="cn.itcast.spring.demo4.MyAspectAnno"></bean>

1.9 其他通知的註解:

@Aspect
public class MyAspectAnno {
    @Before("MyAspectAnno.pointcut1()")
    public void before(){
        System.out.println("前置通知===========");
    }
    @AfterReturning("MyAspectAnno.pointcut2()")
    public void afterReturning(){
        System.out.println("後置通知===========");
    }
    @Around("MyAspectAnno.pointcut3()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("環繞前通知==========");
        Object obj = joinPoint.proceed();
        System.out.println("環繞後通知==========");
    return obj;
    }
@AfterThrowing("MyAspectAnno.pointcut4()")
public void afterThrowing(){
    System.out.println("異常拋出通知========");
}
@After("MyAspectAnno.pointcut4()")
public void after(){
    System.out.println("最終通知==========");
}
@Pointcut("execution(* cn.itcast.spring.demo4.ProductDao.save(..))")
private void pointcut1(){}
@Pointcut("execution(* cn.itcast.spring.demo4.ProductDao.update(..))")
private void pointcut2(){}
@Pointcut("execution(* cn.itcast.spring.demo4.ProductDao.delete(..))")
private void pointcut3(){}
@Pointcut("execution(* cn.itcast.spring.demo4.ProductDao.find(..))")
private void pointcut4(){}
}

案例二:Spring的事務管理完成轉賬的案例

1.案例需求:

完成一個轉賬的功能,需要進行事務的管理,使用 Spring 的事務管理的方式完成.

2.相關知識點

2.1 Spring 的 JDBC 的模板:

2.1.1Spring 提供了很多持久層技術的模板類簡化編程:


2.1.2 創建數據庫和表:

2.1.3引入相關開發包:

Spring 的基本的開發包需要引入的:6 個.


2.1.4創建一個測試類:

@Test
// JDBC 模板的基本使用:
public void demo1(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring_day03");
dataSource.setUsername("root");
dataSource.setPassword("123");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update("insert into account values (null,?,?)", " 會 希
",10000d);
}

2.2將連接池的配置交給 Spring 管理:

2.2.1 Spring 內置的連接池的配置:

【引入 Spring 的配置文件】
【配置內置連接池】
 <!-- 配置 Spring 的內置連接池 -->
 <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql:///spring_day02"/>
 <property name="username" value="root"/>
 <property name="password" value="123"/>
 </bean>
【將模板配置到 Spring 中】
 <!-- 配置 JDBC 模板 -->
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
 <property name="dataSource" ref="dataSource"/>
 </bean>
【編寫測試類】
**** 引入 spring-aop.jar
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo2 {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
public void demo1(){
jdbcTemplate.update("insert into account values (null,?,?)", " 鳳 姐
",10000d);
}
}

2.2.2Spring 中配置 DBCP 連接池:

【引入 dbcp 連接池的 jar 包】


【配置連接池】
 <!-- 配置 DBCP 連接池 -->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql:///spring_day02"/>
 <property name="username" value="root"/>
 <property name="password" value="123"/>
 </bean>

2.2.3配置 c3p0 連接池:

【引入相應的 jar 包】

com.springsource.com.mchange.v2.c3p0-0.9.1.jar


【配置連接池】
 <!-- 配置 C3P0 連接池 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 <property name="driverClass" value="com.mysql.jdbc.Driver"/>
 <property name="jdbcUrl" value="jdbc:mysql:///spring_day02"/>
 <property name="user" value="root"/>
 <property name="password" value="123"/>
 </bean>

2.2.4將數據庫連接的信息配置到屬性文件中:

【定義屬性文件】
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_day02
jdbc.username=root
jdbc.password=123
【引入外部的屬性文件】
一種方式:
 <!-- 引入外部屬性文件: -->
 <bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="location" value="classpath:jdbc.properties"/>
 </bean>
二種方式:
<context:property-placeholder location="classpath:jdbc.properties"/>

2.3 JDBC 模板的 CRUD 的操作:

2.3.1 JDBC 模板 CRUD 的操作:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo3 {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
// 插入操作
public void demo1(){
jdbcTemplate.update("insert into account values (null,?,?)", " 冠 希
",10000d);
}
@Test
// 修改操作
public void demo2(){
jdbcTemplate.update("update account set name=?,money =? where id = ?", "
思雨",10000d,5);
}
@Test
// 刪除操作
public void demo3(){
jdbcTemplate.update("delete from account where id = ?", 5);
}
@Test
// 查詢一條記錄
public void demo4(){
Account account = jdbcTemplate.queryForObject("select * from account where
id = ?", new MyRowMapper(), 1);
System.out.println(account);
}
@Test
// 查詢所有記錄
public void demo5(){
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;
}
}
}

2.4事務的回顧:

2.4.1 什麼是事務:

事務邏輯上的一組操作,組成這組操作的各個邏輯單元,要麼一起成功,要麼一起失敗.

2.4.2 事務特性:

原子性 :強調事務的不可分割.
一致性 :事務的執行的前後數據的完整性保持一致.
隔離性 :一個事務執行的過程中,不應該受到其他事務的干擾
持久性 :事務一旦結束,數據就持久到數據庫

2.4.3 如果不考慮隔離性引發安全性問題:

髒讀 :一個事務讀到了另一個事務的未提交的數據
不可重複讀 :一個事務讀到了另一個事務已經提交的 update 的數據導致多次查詢結果不一致.
虛幻讀 :一個事務讀到了另一個事務已經提交的 insert 的數據導致多次查詢結果不一致.

2.4.4 解決讀問題:設置事務隔離級別

未提交讀 :髒讀,不可重複讀,虛讀都有可能發生
已提交讀 :避免髒讀。但是不可重複讀和虛讀有可能發生
可重複讀 :避免髒讀和不可重複讀.但是虛讀有可能發生.
串行化的 :避免以上所有讀問題.
Mysql 默認:可重複讀
Oracle 默認:讀已提交

2.5 Spring 進行事務管理一組 API

2.5.1 PlatformTransactionManager:平臺事務管理器.

***** 真正管理事務的對象
org.springframework.jdbc.datasource.DataSourceTransactionManager 使用 Spring
JDBC 或 iBatis 進行持久化數據時使用
org.springframework.orm.hibernate3.HibernateTransactionManager 使用
Hibernate 版本進行持久化數據時使用

2.5.2 TransactionDefinition:事務定義信息

事務定義信息:
* 隔離級別
* 傳播行爲
* 超時信息
* 是否只讀

2.5.3 TransactionStatus:事務的狀態

記錄事務的狀態

2.5.4 Spring 的這組接口是如何進行事務管理:

平臺事務管理根據事務定義的信息進行事務的管理,事務管理的過程中產生一些狀態,將這些狀態記
錄到 TransactionStatus 裏面

2.5.5 事務的傳播行爲

PROPAGION_XXX :事務的傳播行爲
 * 保證同一個事務中
PROPAGATION_REQUIRED 支持當前事務,如果不存在 就新建一個(默認)
PROPAGATION_SUPPORTS 支持當前事務,如果不存在,就不使用事務
PROPAGATION_MANDATORY 支持當前事務,如果不存在,拋出異常
* 保證沒有在同一個事務中
PROPAGATION_REQUIRES_NEW 如果有事務存在,掛起當前事務,創建一個新的事務
PROPAGATION_NOT_SUPPORTED 以非事務方式運行,如果有事務存在,掛起當前事務
PROPAGATION_NEVER 以非事務方式運行,如果有事務存在,拋出異常
PROPAGATION_NESTED 如果當前事務存在,則嵌套事務執行

3.案例代碼

3.1 搭建轉賬的環境:

3.1.1 創建業務層和 DAO 的類

public interface AccountService {
public void transfer(String from,String to,Double money);
}
public class AccountServiceImpl implements AccountService {
// 業務層注入 DAO:
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
/**
* from:轉出的賬號
* to:轉入的賬號
* money:轉賬金額
*/
public void transfer(String from, String to, Double money) {
accountDao.outMoney(from, money);
accountDao.inMoney(to, money);
}
}
public interface AccountDao {
public void outMoney(String from,Double money);
public void inMoney(String to,Double money);
}
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);
}


}

3.1.2 配置業務層和 DAO

 <!-- 配置業務層的類 -->
 <bean id="accountService"
class="cn.itcast.transaction.demo1.AccountServiceImpl">
 <property name="accountDao" ref="accountDao"/>
 </bean>


 <!-- 配置 DAO 的類 -->
 <bean id="accountDao" class="cn.itcast.transaction.demo1.AccountDaoImpl">
 <property name="dataSource" ref="dataSource"/>
 </bean>

3.1.3 編寫測試類

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class SpringDemo4 {
@Resource(name="accountService")
private AccountService accountService;
@Test
// 轉賬的測試:
public void demo1(){
accountService.transfer("會希", "鳳姐", 1000d);
}
}

3.2 Spring 的編程式事務(瞭解)

手動編寫代碼完成事務的管理:

3.2.1 配置事務管理器

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

3.2.2 配置事務管理的模板

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

3.2.3 需要在業務層注入事務管理模板

 <!-- 配置業務層的類 -->
 <bean id="accountService"
class="cn.itcast.transaction.demo1.AccountServiceImpl">
 <property name="accountDao" ref="accountDao"/>
 <!-- 注入事務管理模板 -->
 <property name="transactionTemplate" ref="transactionTemplate"/>
 </bean>

3.2.4 手動編寫代碼實現事務管理

public void transfer(final String from, final String to, final Double money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status)
{
accountDao.outMoney(from, money);
int d = 1 / 0;
accountDao.inMoney(to, money);
}
});
}

3.3 Spring 的聲明式事務管理 XML 方式(*****):思想就是 AOP.

不需要進行手動編寫代碼,通過一段配置完成事務管理

3.3.1 引入 AOP 開發的包

aop 聯盟.jar
Spring-aop.jar
aspectJ.jar
spring-aspects.jar

3.3.2 恢復轉賬環境

3.3.3 配置事務管理器

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

3.3.4 配置事務的通知

 <!-- 配置事務的增強 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
 <tx:attributes>
 <!--
isolation="DEFAULT" 隔離級別
propagation="REQUIRED" 傳播行爲
read-only="false" 只讀
timeout="-1" 過期時間
rollback-for="" -Exception
no-rollback-for="" +Exception
-->
 <tx:method name="transfer" propagation="REQUIRED"/>
 </tx:attributes>
 </tx:advice>

3.3.5 配置 aop 事務

 <aop:config>
 <aop:pointcut expression="execution(*
cn.itcast.transaction.demo2.AccountServiceImpl.transfer(..))" id="pointcut1"/>
 <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
 </aop:config>

3.4 Spring 的聲明式事務的註解方式: (*****)

3.4.1 引入 jar 包:

3.4.2 恢復轉賬環境:

3.4.3 配置事務管理器:

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

3.4.4 開啓事務管理的註解:

<!-- 開啓註解事務管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

3.4.5 在使用事務的類上添加一個註解:@Transactional

發佈了44 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章