注解实现AOP

applicationContext.xml

<!--方法三:注解支持-->
    <bean id="point" class="com.shao.diy.PointCut"/>
    <aop:aspectj-autoproxy/>
package com.shao.diy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

//标注这个类是一个切面
@Aspect
public class PointCut {
    @Before("execution(* com.shao.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("-------开始前-------");
    }
    @After("execution(* com.shao.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("-------结束后-------");
    }
    //在环绕中,我们可以给定一个参数,代表我们要处理的介入的点
    @Around("execution(* com.shao.service.UserServiceImpl.*(..))")
    //与切入点匹配的连接点
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("-------环绕前-------");
        Object proceed = joinPoint.proceed();
        System.out.println("-------环绕后-------");
        return proceed;
    }
}

posted @ 2021-10-26 10:18  蘑菇王国大聪明  阅读(42)  评论(0)    收藏  举报