频道栏目
首页 > 资讯 > 其他综合 > 正文

Spring Aspect简单实例

17-09-15        来源:[db:作者]  
收藏   我要投稿

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



    org.aspectj
    aspectjweaver
    1.8.9

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

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 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 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文件




    
    
        
        
        
        
    

最后写测试方法

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文件需要扫描当前包


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 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 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;
//  }

}

结果和上图一样

相关TAG标签
上一篇:Python数据挖掘-文本挖掘
下一篇:计算机网络和互连网概述
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站