关闭

Spring Aspect简单实例

标签: spring
266人阅读 评论(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网站的观点或立场

Spring AOP实例 Junit4单元测试

一、创建maven项目springAOP pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w...
  • shengfakun1234
  • shengfakun1234
  • 2017-01-20 10:49
  • 1780

Spring4-aspect日志管理实例

Spring-常用Jar包[http://pan.baidu.com/s/1dD1t92D]aspect实例所需jar包如上bean配置文件aspect.xml <beans xmlns="http:...
  • baidu_25958185
  • baidu_25958185
  • 2015-02-26 13:46
  • 1259

SpringAOP实例之简单实现(Annotation形式)

Spring除了支持Schema方式配置AOP,还支持注解方式:使用@AspectJ风格的切面声明。    1  启用对@AspectJ的支持        Spring默认不支持@AspectJ...
  • zmx729618
  • zmx729618
  • 2016-04-21 10:26
  • 4479

05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置

转载请注明来源 赖赖的博客导语 没有什么是不可以改变的,换个角度看世界,截然不同! IoC相关的基本内容告一段落,本次介绍Spring的第二个特性,AOP,面向切面编程,术语听起来比较不容易理解,...
  • m0_37038036
  • m0_37038036
  • 2016-12-14 14:56
  • 4013

超简单Spring入门 (二) Aspect-oriented Programming

除了Dependency Injection, Spring还能做什么呢? 回答是aspect-oriented programming。 当我们在写程序的时候,除了实现core logic之外,...
  • OnlyQi
  • OnlyQi
  • 2011-07-28 16:00
  • 1134

Spring aop 记录操作日志 Aspect 源码

  • 2018-01-04 17:27
  • 9.35MB
  • 下载

SPRING:aspect和advisor区别

  • 2012-01-29 16:18
  • 22KB
  • 下载

Packt.Spring.2.5.Aspect.Oriented.Programming

  • 2010-09-23 18:42
  • 7.00MB
  • 下载

Spring aspect .jar包 (3.0版本)

  • 2012-11-28 16:13
  • 15.34MB
  • 下载

Spring 2.5 Aspect-Oriented Programming source code

  • 2011-08-23 05:09
  • 264KB
  • 下载
    个人资料
    • 访问:1100次
    • 积分:67
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档