hooyantsing's Blog

第113次课程_Spring部分作业检查、复习及指导

字数统计: 989阅读时长: 5 min
2020/02/27

源辰76班

第113次课程

2020.02.27

内容

Spring部分作业检查、复习及指导

项目路径:****G:\YC74\200227_Spring部分作业检查、复习及指导\teach

1.Spring事务控制 关于rollbackFor的使用

@Transactional默认遇到RuntimeException才执行回滚操作

可以在参数列表中声明,当抛出非运行期异常时也执行回滚操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Transactional(rollbackFor=BusiException.class)
public void deposit(Account account) throws  BusiException {
       System.out.println("模拟存款业务!");
       aDao.update(account);
       Record r = new Record();
       r.setAccountId(account.getId());
       r.setMoney(account.getMoney());
       
       //运行期异常  测试事务
       //int i = 1/0;
       
       if(account.getMoney() < 10000) {
           throw new BusiException();
       }
       
       rDao.insert(r);
}

2.JDK动态代理 和 CGLIB动态代理

代理:AOP中,通过切面技术,为源方法加上增强功能而包装出的代理方法

.png

JDK动态代理:需要继承接口

-1.png

I:共同接口

P:代理对象

R:真实对象

当程序调用R,由于R和P实现了同一个I,所有将P返回。

IAccountBiz.java

I

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.yc.spring.bank.biz;

import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.yc.spring.bank.bean.Account;
import com.yc.spring.bank.bean.Record;

public interface IAccountBiz {

    void deposit(Account account) throws BusiException;

    void withdraw(Account account);

    void transfer(Account account1, Account account2);

    List<Record> details();

    List<Record> details1();

}

AccountBiz.java

R

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.yc.spring.bank.biz;

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.Transactional;
import com.yc.spring.bank.bean.Account;
import com.yc.spring.bank.bean.Record;
import com.yc.spring.bank.dao.AccountDao;
import com.yc.spring.bank.dao.RecordDao;

@Transactional
public class AccountBiz implements IAccountBiz {
    
    @Autowired
    private AccountDao aDao;
    @Autowired
    private RecordDao rDao;
    
    /* (非 Javadoc)
     * @see com.yc.spring.bank.biz.IAccountBiz#deposit(com.yc.spring.bank.bean.Account)
     */
    @Override
    @Transactional(rollbackFor=BusiException.class)
    public void deposit(Account account) throws BusiException {
        System.out.println("模拟存款业务!");
        aDao.update(account);
        Record r = new Record();
        r.setAccountId(account.getId());
        r.setMoney(account.getMoney());
        
        //运行期异常  测试事务
        //int i = 1/0;
        
        if(account.getMoney() < 10000) {
            throw new BusiException();
        }
        
        rDao.insert(r);
    }
    
    /* (非 Javadoc)
     * @see com.yc.spring.bank.biz.IAccountBiz#withdraw(com.yc.spring.bank.bean.Account)
     */
    @Override
    public void withdraw(Account account) {
        System.out.println("模拟取款业务!");
        aDao.update(account);
        rDao.insert(new Record());
    }
    
    /* (非 Javadoc)
     * @see com.yc.spring.bank.biz.IAccountBiz#transfer(com.yc.spring.bank.bean.Account, com.yc.spring.bank.bean.Account)
     */
    @Override
    public void transfer(Account account1, Account account2) {
        System.out.println("模拟转账业务!");
        aDao.update(account1);
        aDao.update(account2);
        rDao.insert(new Record());
        rDao.insert(new Record());
    }
    
    /* (非 Javadoc)
     * @see com.yc.spring.bank.biz.IAccountBiz#details()
     */
    @Override
    public List<Record> details(){
        return new ArrayList<Record>();
    }
    
    /* (非 Javadoc)
     * @see com.yc.spring.bank.biz.IAccountBiz#details1()
     */
    @Override
    public List<Record> details1(){
        int a = 1/0;
        return new ArrayList<Record>();
    }
    
}

BankTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.yc.spring.bank;
import java.sql.Date;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.jdbc.core.JdbcTemplate;
import  org.springframework.test.context.ContextConfiguration;
import  org.springframework.test.context.junit4.SpringRunner;
import com.yc.spring.bank.action.BankAction;
import com.yc.spring.bank.bean.Account;
import com.yc.spring.bank.biz.BusiException;
import com.yc.spring.bank.biz.IAccountBiz;
import com.yc.spring.bank.dao.AccountDao;
@RunWith(SpringRunner.class)
@ContextConfiguration("/bank-beans.xml")
public class BankTest {
    
    @Autowired
    private BankAction bankAction;
    
    @Resource
    private IAccountBiz aBiz;
  
    @Test
    public void test3() throws BusiException  {
       Account account = new Account();
       account.setId(1);
       account.setMoney(500d);
       aBiz.deposit(account);
    }
}

CGLIB动态代理:Spring默认代理方式

3.IOC 控制反转

控制反转:由Spring创建对象,不用自己来写”A a = new A();”

DI:依赖注入

4.如何实例化一个ApplicationContext对象

读取xml文件(xml方式配置文件)

ApplicationContext ac = new ClassPathXmlApplicationContext(“beans.xml”);

读取java文件(注解方式配置文件)

ApplicationContext ac = new AnnocationApplicationContext(“注解配置类.class”);

配置Spring测试类

@RunWith(SpringRunner.class)

@ContextConfiguration(“/bank-beans.xml”)

5.Spring事务管理分类

1.编成式事务:try {业务} catch {回滚} finally {关闭}

2.声明式事务:所学的Spring注解@Transactional

6.JoinPoint与ProceedingJoinPoint有什么联系?用于什么场景?

JoinPoint 是 ProceedingJoinPoint 的父类

JoinPoint 应用于”非环绕增强“的场景下

    JoinPoint 只是对 方法对象 和 参数 的封装

ProceedingJoinPoint 应用于”环绕增强“的场景下

    环绕增强的业务方法不会自动的执行

    jpj.proceed(); // 执行业务方法

CATALOG