hooyantsing's Blog

第110次课程_Spring_AOP

字数统计: 1.1k阅读时长: 6 min
2020/02/21

源辰76班

第110次课程

2020.02.21

内容

Spring_AOP

项目路径:

1.[注解方式]自动装配 和 [xml方式]包扫描

案例是以 注解方式 和 xml方式 混合

自动装配部分

方法注解:

1
2
3
@Controller    //Action 控制器
@Service       //Biz    
@Repository    //Dao

属性注解:

1
@Autowired    //private 属性

BankAction.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.yc.spring.bank.action;
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.stereotype.Controller;
import com.yc.spring.bank.bean.Account;
import com.yc.spring.bank.biz.AccountBiz;
import com.yc.spring.bank.biz.RecordBiz;
/**
* SpringMVC 控制器
*/
@Controller
public class BankAction {
    @Autowired
    private AccountBiz aBiz;
    @Autowired
    private RecordBiz rBiz;
    /**
     * 存款
     * @param account
     * @return
     */
    public String deposit(Account account) {
       aBiz.deposit(account);
       return "details";
    }
    /**
     * 取款
     * @param account
     * @return
     */
    public String withdraw(Account account)  {
       aBiz.withdraw(account);
       return "details";
    }
    /**
     * 转账
     * @param account1
     * @param account2
     * @return
     */
    public String transfer(Account account1,  Account account2) {
       aBiz.transfer(account1, account2);
       return "details";
    }
    /**
     * 查询交易明细
     * @param accountId
     * @return
     */
    public String details(int accountId) {
       rBiz.queryByAccountId(accountId);
       return "details";
    }
}

AccountBiz.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.yc.spring.bank.biz;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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;

@Service
public class AccountBiz {
    
    @Autowired
    private AccountDao aDao;
    @Autowired
    private RecordDao rDao;
    
    /**
     *     存款业务
     */
    public void deposit(Account account) {
        System.out.println("模拟存款业务!");
        aDao.update(account);
        rDao.insert(new Record());
    }
    
    /**
     *     取款业务
     */
    public void withdraw(Account account) {
        System.out.println("模拟取款业务!");
        aDao.update(account);
        rDao.insert(new Record());
    }
    
    /**
     *     转账业务
     */
    public void transfer(Account account1, Account account2) {
        System.out.println("模拟转账业务!");
        aDao.update(account1);
        aDao.update(account2);
        rDao.insert(new Record());
        rDao.insert(new Record());
    }
    
}

包扫描部分

xml配置文件中使用

bank-beans.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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:p="http://www.springframework.org/schema/p"
    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">
     
     <!-- 相当于java注解:@ComponentScan("com.yc.spring.bank") 包扫描 -->
    <context:component-scan  base-package="com.yc.spring.bank"></context:component-scan>

</beans>

测试部分

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
package com.yc.spring.bank;
import org.junit.Test;
import org.junit.runner.RunWith;
import  org.springframework.beans.factory.annotation.Autowired;
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;

//引入SpringRunner驱动,让程序可以自动装载
@RunWith(SpringRunner.class)
//引入xml配置文件
@ContextConfiguration("/bank-beans.xml")
public class BankTest {
    
    @Autowired
    private BankAction bankAction;
    
    @Test
    public void test1() {
       bankAction.deposit(new Account());
    }
}

2.AOP [XML方式]配置

关系图

切面 = 切点 + 通知(增强)

.png

bank-beans.xml

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
<?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:p="http://www.springframework.org/schema/p"
    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">
     
     <!-- 相当于java注解:@ComponentScan("com.yc.spring.bank") 包扫描 -->
     <context:component-scan  base-package="com.yc.spring.bank"></context:component-scan>
     
     <!-- AOP XML 配置方案 -->
     <aop:config>
        <!-- 切点定义:aop:pointcut
                   技术来源:AspectJ
                   execution (*  com.xyz.myapp.service.*.*(..))
                   执行方法           (返回值                   .包名.类名(所有参数列表))*表示所有
        -->
        <aop:pointcut expression="execution  (* com.yc.spring.bank.biz.*.*(..))"  id="bankBiz"/>
        <!-- 切面定义:aop:aspect -->
        <aop:aspect id="myAspect"  ref="logAdvice">
           <!-- 通知位置定义 -->
           <aop:before  pointcut-ref="bankBiz" method="before"/>
        </aop:aspect>      
     </aop:config>
     
     <!-- 增强类/通知类定义 -->
     <bean id="logAdvice"  class="com.yc.spring.bank.aop.LogAdvice"></bean>
     
     <!-- AspectJ自动代理 -->
     <aop:aspectj-autoproxy />
</beans>

增强类/通知类

LogAdvice.java

[JoinPoint jp]可以判断是那个方法来调用的增强类

1
2
3
4
5
6
7
8
9
10
11
package com.yc.spring.bank.aop;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
public class LogAdvice {
    
    public void before(JoinPoint jp) {
       System.out.println("方法签名" +  jp.getSignature());
       System.out.println("方法参数列表" +  Arrays.toString(jp.getArgs()));
       System.out.println("=====before=====");
    }
}

AOP依赖

pom.xml

1
2
3
4
5
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.3.17.RELEASE</version>
</dependency>
CATALOG