关闭

Spring Aspect简单实例

标签: spring
195人阅读 评论(0) 收藏 举报
分类:

首先是在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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:472次
    • 积分:62
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档