Spring-AOP @AspectJ进阶之访问连接点信息

来源:互联网 发布:昆明java 编辑:程序博客网 时间:2024/06/03 19:57

  • 概述
    • JoinPoint
    • ProceedingJoinPoint
  • 实例

概述

AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点对象,该类是JoinPoint的子接口。

任何一个增强方法都可以通过将第一个入参声明为JoinPoint访问到连接点上下文的信息。

我们先来了解一下这两个接口的主要方法:

JoinPoint

  • java.lang.Object[] getArgs():获取连接点方法运行时的入参列表;

  • Signature getSignature() :获取连接点的方法签名对象;

  • java.lang.Object getTarget() :获取连接点所在的目标对象;

  • java.lang.Object getThis() :获取代理对象本身;


ProceedingJoinPoint

ProceedingJoinPoint继承JoinPoint子接口,它新增了两个用于执行连接点方法的方法:

  • java.lang.Object proceed() throws
    java.lang.Throwable:通过反射执行目标对象的连接点处的方法;

  • java.lang.Object proceed(java.lang.Object[] args) throws
    java.lang.Throwable:通过反射执行目标对象连接点处的方法,不过使用新的入参替换原来的入参。


实例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster


这里写图片描述

业务类

package com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint;import org.springframework.stereotype.Component;/** *  *  * @ClassName: LogicService *  * @Description: @Component标注的Bean *  * @author: Mr.Yang *  * @date: 2017年9月12日 上午1:09:38 */@Componentpublic class LogicService {    public void dealLogic(String bussiness) {        System.out.println("deal Logic:" + bussiness);    }}

编写切面

package com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;/** *  *  * @ClassName: JoinPointAspect *  * @Description: @Aspect标注的切面 *  * @author: Mr.Yang *  * @date: 2017年9月12日 上午1:10:40 */@Aspectpublic class JoinPointAspect {    // ①环绕增强    @Around("execution(* dealLogic(..)) && target(com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint.LogicService)")    public void crossCodeCutting(ProceedingJoinPoint pjp) throws Throwable { // ②声明连接点入参        System.out.println("-------ProceedingJoinPoint begin----------");        // ③访问连接点信息        System.out.println("arg[0]:" + pjp.getArgs()[0]);        System.out.println("signature:" + pjp.getTarget().getClass());        // ④通过连接点执行目标对象的方法        pjp.proceed();        System.out.println("-------ProceedingJoinPoint end----------");    }}

在①处,我们声明了一个环绕增强,在②处增强方法的第一个入参声明为PreceedingJoinPoint类型(注意一定要在第一个位置),在③处,我们通过连接点对象pjp访问连接点的信息。在④处,我们通过连接点调用目标对象的方法

将业务Bean和切面配置到配置文件中

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd"><!-- (1)声明Context命名空间以及Schema文件   (2)扫描类包以及应用注解定义的bean --><context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint"/><!-- 基于@AspectJ切面的驱动器 --><aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面类 --><bean class="com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint.JoinPointAspect"/></beans>

测试类

package com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class JoinPointAspectTest {    @Test    public void test() {        ApplicationContext ctx = new ClassPathXmlApplicationContext(                "classpath:com/xgj/aop/spring/advisor/aspectJAdvance/joinPoint/conf-joinPoint.xml");        LogicService logicService = ctx.getBean("logicService",                LogicService.class);        logicService.dealLogic("PROGRAMMING");    }}

运行结果

2017-09-12 01:19:27,120  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@292898f5: startup date [Tue Sep 12 01:19:27 BOT 2017]; root of context hierarchy2017-09-12 01:19:27,196  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJAdvance/joinPoint/conf-joinPoint.xml]-------ProceedingJoinPoint begin----------arg[0]:PROGRAMMINGsignature:class com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint.LogicServicedeal Logic:PROGRAMMING-------ProceedingJoinPoint end----------