AOP的学习

注意:不惊动原始设计

一、部署依赖

        AOP开发默认导入

        

<dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

二、具体步骤

@Aspect 告诉Spring 这个类是aop

(1)注意Pointcut后的格式

(2)注意aop(){} 是一个空的方法体,后面不加分号

@Component
@Aspect
public class MyAdvice {
    //4 定义切入点
    @Pointcut("execution(void com.itheima.dao.BookDao.update())")
    private void aop(){}

    //5绑定切入点和共性功能
    @Before("pt()")
    public void method(){
        System.out.println(System.currentTimeMillis());
    }
}

之后在SpringConfig类需要

@EnableAspectJAutoProxy
public class SpringConfig {
}

开启Spring对AOP注解驱动的支持

三、AOP的切入表达式

四、通知类型

环绕通知:

 通过ProceedingJoinPoint 对象对原始连接点进行调用

 此返回值为Object 

五、通知获取数据

  @Before("pt()")
    public void before(JoinPoint jp){
        Object[] args = jp.getArgs();
        System.out.println(Arrays.toString(args));
        System.out.println("before advice");
    }

 前置通知和后置通知获取方法中参数的方式一样,通过JoinPoint对象来获取即可,获取到的类型是Objtct[] 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值