十二.mian向切面编程---方法san
1.使用注解
<!-- 配置方式三:注解-->
<!-- 自动代理 默认是使用jdk的代理 false是jdk的,true是cglib
通过aop命名空间的<aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。
-->
<aop:aspectj-autoproxy proxy-target-class="false"/>
2.注解+java代码:
package com.why.aop;
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;
import org.springframework.stereotype.Component;
/**
* @program: AOP
* @description:
* @author: @why
* @create: 2020-09-01 16:04
**/
@Aspect
@Component
public class Log_annotation {
//在执行方法之前切入
@Before("execution(* com.why.serivce.ServiceIml.*(..))")
public void before()
{
System.out.println("我在你前面切入");
}
//在执行方法之后切入
@After("execution(* com.why.serivce.ServiceIml.*(..))")
public void after()
{
System.out.println("人家在你下面啦");
}
//环绕增强中 我们可以给定一个参数 代表我们获取要处理的切入点
//@around=@before+@after
@Around("execution(* com.why.serivce.ServiceIml.add(..))")
public void Around(ProceedingJoinPoint point) throws Throwable {
System.out.println("执行方法之前要执行的代码");
point.proceed();//执行对应的方法
System.out.println(point.getSignature());
System.out.println("执行方法之后要执行的代码");
}
}

浙公网安备 33010602011771号