Spring Aop源码分析

最近看了SpringAop的源码实现  大概记录一下aop的源码流程

创建一个最简单的一个测试类

package com.zcg.learn.Test;

import org.aopalliance.aop.Advice;
import org.junit.Test;
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.tests.aop.advice.CountingAfterReturningAdvice;
import org.springframework.tests.aop.advice.CountingBeforeAdvice;

import com.zcg.learn.UserService;
import com.zcg.learn.UserServiceImpl;

/**
* SpringAop源码分析测试类
* @author zcg
* 2018/3/1
*
*/
public class SpringAopTest {

/**
* 创建代理
*/
@Test
public void createProxyTest() {
Object target = new UserServiceImpl();
ProxyFactory pf = new ProxyFactory(target);
//CountingBeforeAdvice 前置通知计数器
CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
pf.addAdvice(countingBeforeAdvice);
UserService service = (UserService) pf.getProxy();
service.addUser();
}

/**
* 动态添加 移除通知
*/
@Test
public void createProxyTest2() {
Object target = new UserServiceImpl();
ProxyFactory pf = new ProxyFactory(target);
UserService service = (UserService) pf.getProxy();
Advised advised = (Advised) service;
CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
CountingAfterReturningAdvice countingAfterReturningAdvice = new CountingAfterReturningAdvice();
advised.addAdvice(countingAfterReturningAdvice);
advised.addAdvice(countingBeforeAdvice);
service.addUser();
advised.removeAdvice(countingAfterReturningAdvice);
service.addUser();
}

@Test
public void createProxyAspectJByTest() {
Object target = new UserServiceImpl();
AspectJProxyFactory pf = new AspectJProxyFactory(target);
AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
advisor.set)");
CountingBeforeAdvice counting = new CountingBeforeAdvice();
advisor.setAdvice(counting);
pf.addAdvisor(advisor);
UserService userService = pf.getProxy();
userService.addUser();
/**
* advice 通知拦截器
* advisor 通知加切入点适配器
*/


}

}

其中测试方式

@Test
public void createProxyTest() {
Object target = new UserServiceImpl();


ProxyFactory pf = new ProxyFactory(target);
//CountingBeforeAdvice 前置通知计数器
CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
pf.addAdvice(countingBeforeAdvice);
UserService service = (UserService) pf.getProxy();
service.addUser();
}

1.target 以构造参数的形式放入在ProxyFactory中,实际上将该tartget放入在AdvisedSupport类中

槑槑,JAVA,自动化测试,微服务,框架,技术

2.countingBeforeAdvice 为Spring Aop自带的前置通知计数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值