Spring Aspect简单实例

首先是在maven的pom中配置需要安装的jar,在原有的spring配置中添加

<!-- aop aspect -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.9</version>
</dependency>

添加需要被代理的方法,
先写一个接口

package com.north.spring.aop.impl;

public interface Arti {
    public int add(int i, int j);
    public int sub(int i, int j);
    public int div(int i, int j);
    public int mul(int i, int j);
}

再写一个实现类

package com.north.spring.aop.xml;

public class ArtiImpl implements Arti {

    @Override
    public int add(int i, int j) {
        // TODO Auto-generated method stub
        int result = i + j;
        return result;
    }

    @Override
    public int sub(int i, int j) {
        // TODO Auto-generated method stub
        int result = i - j;
        return result;
    }

    @Override
    public int div(int i, int j) {
        // TODO Auto-generated method stub
        int result = i / j;
        return result;
    }

    @Override
    public int mul(int i, int j) {
        // TODO Auto-generated method stub
        int result = i * j;
        return result;
    }

}

然后写一个代理的方法

package com.north.spring.aop.xml;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;


public class LoggingAspect {

    private void declarePointExpresson() {}
    //前置
    public void beforeMethod(JoinPoint joinpoint) {
        String name = joinpoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinpoint.getArgs());


        System.out.println("The method "+name+" begin with"+args);

    }
    //后置
    public void afterMethod(JoinPoint joinpoint) {
        String name = joinpoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinpoint.getArgs());


        System.out.println("The method "+name+" end with"+args);
    }

    //返回
    public void afterReturning(JoinPoint joinpoint, Object result) {
        String name = joinpoint.getSignature().getName();
        System.out.println("The method "+name+" end with "+ result);
    }

    //异常
    public void afterThrowing(JoinPoint joinpoint, Exception ex) {
        System.out.println("The exception occaur at "+ ex);
    }


//  @Around(value = "execution(* com.north.spring.aop.impl.*.*(int,int))")
//  public Object around(ProceedingJoinPoint proceed) {
//      Object result = null;
//      String name = proceed.getSignature().getName();
//      Object[] args = proceed.getArgs();
//      try {
//          //前置
//          System.out.println("the Method "+name+" start with "+ Arrays.asList(args));
//          result = proceed.proceed();
//          //返回
//          System.out.println("the method "+name+" end with "+result);
//          
//      } catch (Throwable e) {
//          // TODO: handle exception
////            e.printStackTrace();
            //异常
//          System.out.println("the method "+name +" occaur at "+e);
//      }
//      //后置
//      System.out.println("end ");
//      return result;
//  }

}

配置xml文件

<bean id="loggingAop" class="com.north.spring.aop.xml.LoggingAspect"></bean>

<aop:config>
    <aop:pointcut expression="execution(* com.north.spring.aop.xml.Arti.*(int,int))" id="ponitcut"/>
    <aop:aspect ref="loggingAop">
        <aop:before method="beforeMethod" pointcut-ref="ponitcut"/>
        <aop:after method="afterMethod" pointcut-ref="ponitcut"/>
        <aop:after-returning method="afterReturning" pointcut-ref="ponitcut" returning="result"/>
        <aop:after-throwing method="afterThrowing" pointcut-ref="ponitcut" throwing="ex"/>
    </aop:aspect>
</aop:config>

最后写测试方法

package com.north.spring.aop.xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAop {
    public static void main(String[] args) {
        ApplicationContext ctx =  new ClassPathXmlApplicationContext("application-xml.xml");

        Arti arti = (Arti) ctx.getBean("arti");

        int result = arti.add(3, 6);

        System.out.println(result);

        result = arti.div(11, 0);
        System.out.println(result);
    }
}

打印结果
这里写图片描述

2.通过注解配置,另外的一样,只是代理的方法和xml文件不同,需要加上注解
XML文件需要扫描当前包

<context:component-scan base-package="com.north.spring.aop.impl"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
package com.north.spring.aop.impl;

import java.util.Arrays;
import java.util.List;

import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

    @Pointcut("execution(public int com.north.spring.aop.impl.*.*(int,int))")
    private void declarePointExpresson() {}

    @Before("declarePointExpresson()")
    public void beforeMethod(JoinPoint joinpoint) {
        String name = joinpoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinpoint.getArgs());


        System.out.println("The method "+name+" begin with"+args);

    }
//  
    @After("declarePointExpresson()")
    public void afterMethod(JoinPoint joinpoint) {
        String name = joinpoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinpoint.getArgs());


        System.out.println("The method "+name+" end with"+args);
    }


    @AfterReturning(value="declarePointExpresson()",
            returning="result")
    public void afterReturning(JoinPoint joinpoint, Object result) {
        String name = joinpoint.getSignature().getName();
        System.out.println("The method "+name+" end with "+ result);
    }

    @AfterThrowing(value="declarePointExpresson()",
            throwing="ex")
    public void afterThrowing(JoinPoint joinpoint, Exception ex) {
        System.out.println("The exception occaur at "+ ex);
    }


//  @Around(value = "execution(* com.north.spring.aop.impl.*.*(int,int))")
//  public Object around(ProceedingJoinPoint proceed) {
//      Object result = null;
//      String name = proceed.getSignature().getName();
//      Object[] args = proceed.getArgs();
//      try {
//          //前置
//          System.out.println("the Method "+name+" start with "+ Arrays.asList(args));
//          result = proceed.proceed();
//          
//          System.out.println("the method "+name+" end with "+result);
//          
//      } catch (Throwable e) {
//          // TODO: handle exception
            e.printStackTrace();
//          System.out.println("the method "+name +" occaur at "+e);
//      }
//      
//      System.out.println("end ");
//      return result;
//  }

}

结果和上图一样

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值