AOP面向切面编程

AOP Aspect Oriented Programming 面向切面编程。
OOP Object Oriented Programming 面向对象编程

1、AOP 加入4个特殊的jar包

spring-aop-4.2.5.RELEASE.jar
spring-aspects-4.2.5.RELEASE.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar

2、在xml文件里引入aop命名空间

<!-- 使用Aspectj配置起作用 为注解的Java 类(真正想要执行的类)生成代理对象-->   
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

3、对切面类添加注解

@Component//不仅需要将此类加入到AOC容器里面
@Aspect//还需要将其申明为一个切面

4、如需在调用真正的类的方法之前执行当前切面类的方法,则 需对其添加如下注解,即前置通知

@Before("execution(public int com.xiyou.aop.helloword.Calculate.add(int , int))") 

注:都是根据真正的类的方法写的,可以从方法那拷过来

@Before("execution(修饰符 返回值 包名.接口名(上面方法没有).类名(参数类型)") 

可以使用*号代替上面的一切 如:

@Before("execution(* 包名.接口名(上面方法没有).*(..)") 
第一个*表示任意修饰符、任意返回值
第二个*任意类
..代表可变参

再如最强的:

@Before("execution(* *.*(..)")
第一个*代表匹配任意修饰符及任意返回值
第二个*代表任意类的对象
第三个 * 代表任意方法
参数列表中的 ..  匹配任意数量的参数

5、得到该连接点的方法的方法名和参数值

即定义一个JoinPoint,可以获取该方法的细节

@Before("execution(* *.*(..)")
public void beforeMethod(JoinPoint joinPoint){
     String methodName = joinPoint.getSignature().getName();
     List<Object> args = Arrays.*asList*(joinPoint.getArgs()); 
}

6、声明切入点表达式使代码更简洁使用

使用@Pointcut 来声明切入点表达式

/**
* 定义一个方法, 用于声明切入点表达式. 
* 该方法中可以添入其他常用的代码. 
* 后面的其他通知直接使用方法名来引用当前的切入点表达式. 
*/
@Pointcut("execution(* 包名.*(..))")
public void declareJointPointExpression(){}
/*
 * 后面的其他通知直接使用方法名来引用当前的切入点表达式. 
 */
@Before("declareJointPointExpression()") 

7、同理有其它通知

为了简便使用的上面申明的@Pointcut

1)、@After后置通知,在方法结束之后执行(出现异常也会执行)
2)、@ArterRunning返回通知,在方法返回结果之后执行,可以访问到方法的返回值

格式:

@AfterReturning(value=execution("方法名"),returning="result")

e.g.

@AfterReturning(value="declareJointPointExpression()",returning="result")
public void afterReturning(JoinPoint joinPoint, Object result){
    String methodName = joinPoint.getSignature().getName();
    System.out.println("The method " + methodName + " ends with " + result);
}
3)、@AfterThrowing异常通知,在方法出现异常后执行

e.g.

@AfterThrowing(value="declareJointPointExpression()",throwing="e")
public void afterThrowing(JoinPoint joinPoint, Exception e){
    String methodName = joinPoint.getSignature().getName();
    System.out.println("The method " + methodName + " occurs excetion:" + e);
}

4)、@Around围绕着方法执行 最强大的 但不是最常用的
/* 环绕通知需要携带 ProceedingJoinPoint 类型的参数. 
 * 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法
 * 且环绕通知必须有返回值, 返回值即为目标方法的返回值           
 * ProceedingJoinPoint . 它是 JoinPoint 的子接口
 */
@Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pjd){
    
    Object result = null;
    String methodName = pjd.getSignature().getName();
    
    try {
        //前置通知
        System.out.println("The method " + methodName +
                                " begins with " + Arrays.asList(pjd.getArgs()));
        //执行目标方法
        result = pjd.proceed();
        //返回通知
        System.out.println("The method " + methodName + " ends with " + result);
    } catch (Throwable e) {
        //异常通知
        System.out.println("The method " + methodName + " occurs exception:" + e);
        throw new RuntimeException(e);
    }
    //后置通知
    System.out.println("The method " + methodName + " ends");
    
    return result;
}

注意:

在环绕通知中需要明确调用ProceedingJoinPoint 的 proceed() 方法来执行被代理的方法
如果忘记这样做就会导致通知被执行了, 但目标方法没有被执行
环绕通知的方法需要返回目标方法执行之后的结果
即调用joinPoint.proceed(); 的返回值, 否则会出现空指针异常

8、切面的优先级用

@Order(index)注解到切面类 index 越小优先级越高

9、基于配置文件xml

<!-- 先将用到的类交给IOC 容器管理 -->
<bean id="calculate" class="com.xiyou.aop.helloword.xml.Calculate"></bean>

<bean id="loggingAspect" class="com.xiyou.aop.helloword.xml.LoggingAspect"></bean>

<aop:config>
    <!-- 配置切面表达式 -->
    <aop:pointcut expression="execution(* 包名.*(int , int))"  id="pointcut"/>

    <!-- 配置前面通知 -->
    <aop:aspect ref="loggingAspect" order="2" >
        <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
    </aop:aspect>
</aop:config>

同理可以配置其它通知

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容