AOP注解

@Component("target")
public class Target implements TargetInterface {
public void save() {
System.out.println("save running.....");
//int i = 1/0;
}
}

@Component("myAspect")
public class MyAspect {
public void before(){
System.out.println("前置增强..........");
}
}

 在切面类中使用注解配置织入关系

@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {
//配置前置通知
@Before("execution(* com.itheima.anno.*.*(..))")
public void before(){
System.out.println("前置增强..........");
}
}

<!--组件扫描-->
<context:component-scan base-package="com.itheima.anno"></context:component-scan>

<!--aop自动代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

 

 

 

@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {
//Proceeding JoinPoint: 正在执行的连接点===切点
//@Around("execution(* com.itheima.anno.*.*(..))")
@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前增强....");
Object proceed = pjp.proceed();//切点方法
System.out.println("环绕后增强....");
return proceed;
}

//@After("execution(* com.itheima.anno.*.*(..))")
@After("MyAspect.pointcut()")
public void after(){
System.out.println("最终增强..........");
}

//定义切点表达式
@Pointcut("execution(* com.itheima.anno.*.*(..))")
public void pointcut(){}
}

posted @ 2022-07-02 17:49  小松2739  阅读(29)  评论(0编辑  收藏  举报