用 AspectJ 注解声明切面:

  • 要在 Spring 中声明 AspectJ 切面 , 只需要在 IOC 容器中将切面声明为 bean 实例。当在 Spring IOC 容器中初始化 AsjectJ 切面之后 , Spring IOC 容器就会为那些与 AspectJ 切面相匹配的 bean 创建代理。
  • 在 ApectJ 注解中 , 切面只是一个带有 @Asject 注解的 Java 类。
  • 通知是标注有某种注解的简单的 Java 方法。
  • AspectJ 支持 5 种类型的通知注解:
    1. @Before:前置通知 , 在方法执行之前执行。
    2. @After:后置通知 , 在方法执行之后执行。
    3. @AfterReturning:返回通知 , 在方法返回结果之后执行。
    4. @AfterThrowing:异常通知 , 在方法抛出异常之后。
    5. @Around:环绕通知 , 围绕着方法执行。

前置通知:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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"> <!-- IOC 扫描包 -->
<context:component-scan base-package="com.itdoc.spring.aop.impl"></context:component-scan>
<!-- 使 AspectJ 注解起作用, 自动为匹配的类生成代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>

注意:一定要添加 aop 命名空间。

 package com.itdoc.spring.aop.impl;

 import org.springframework.stereotype.Component;

 /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-03 19:34
*/
@Component
public interface Arithmetic { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); }
 package com.itdoc.spring.aop.impl;

 import org.springframework.stereotype.Component;

 /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-03 19:35
*/
@Component("arithmetic")
public class ArithmeticImpl implements Arithmetic {
@Override
public int add(int i, int j) {
int result = i + j;
return result;
} @Override
public int sub(int i, int j) {
int result = i - j;
return result;
} @Override
public int mul(int i, int j) {
int result = i * j;
return result;
} @Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}
 package com.itdoc.spring.aop.impl;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; import java.util.Arrays; /**
* http://www.cnblogs.com/goodcheap
* 声明为一个切面的类: 需要把该类放到 IOC 容器中, 再声明为一个切面。
* @author: Wáng Chéng Dá
* @create: 2017-03-03 21:37
*/
@Aspect
@Component
public class LoggingAspect { //声明该方法为前置通知: 在目标方法执行之前开始执行。
@Before("execution(* com.itdoc.spring.aop.impl.*.*(..))")
public void beforeMethod(JoinPoint joinPoint) {
Object methodName = joinPoint.getSignature().getName();
Object args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " + methodName + " begins with " + args);
}
}

@Before("execution(* com.itdoc.spring.aop.impl.*.*(..))")

  • 第一颗 * :代表任意修饰符 , 任意返回值。
  • 第二颗 * :代表任意对象。
  • 第三颗 * :代表任意方法。
  • 最后的 .. :代表任意参数。

注意:在 AspectJ 中 , 切点表达式可以通过操作符 && , || , ! 结合起来。

 package com.itdoc.spring.aop.impl;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-03 21:32
*/
public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Arithmetic arithmetic = (Arithmetic) ctx.getBean("arithmetic");
System.out.println("result = " + arithmetic.add(9, 5));
System.out.println("result = " + arithmetic.sub(9, 5));
}
}

控制台输出:

The method add begins with [9, 5]
result = 14
The method sub begins with [9, 5]
result = 4

后置通知:

  • 后置通知是在连接点完成之后执行的 , 即连接点返回结果或者抛出异常的时候。
  • 一个切面可以包括一个或者多个通知。
 package com.itdoc.spring.aop.impl;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; import java.util.Arrays; /**
* http://www.cnblogs.com/goodcheap
* 声明为一个切面的类: 需要把该类放到 IOC 容器中, 再声明为一个切面。
* @author: Wáng Chéng Dá
* @create: 2017-03-03 21:37
*/
@Aspect
@Component
public class LoggingAspect { //声明该方法为前置通知: 在目标方法执行之前开始执行。
@Before("execution(* com.itdoc.spring.aop.impl.*.*(..))")
public void beforeMethod(JoinPoint joinPoint) {
Object methodName = joinPoint.getSignature().getName();
Object args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " + methodName + " begins with " + args);
} //声明该方法为后置通知: 在目标方法执行之后(无论是否发生异常)开始执行。
@After("execution(* com.itdoc.spring.aop.impl.*.*(..))")
public void afterMethod(JoinPoint joinPoint) {
Object methodName = joinPoint.getSignature().getName();
Object args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " + methodName + " ends with " + args);
}
}
 package com.itdoc.spring.aop.impl;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-03 21:32
*/
public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Arithmetic arithmetic = (Arithmetic) ctx.getBean("arithmetic");
System.out.println("result = " + arithmetic.add(9, 5));
System.out.println("result = " + arithmetic.div(9, 0));
}
}

控制台输出:

Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.itdoc.spring.aop.impl.ArithmeticImpl.div(ArithmeticImpl.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:43)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy8.div(Unknown Source)
at com.itdoc.spring.aop.impl.Main.main(Main.java:19)
The method add begins with [9, 5]
The method add ends with [9, 5]
result = 14
The method div begins with [9, 0]
The method div ends with [9, 0]

返回通知:

无论连接点是正常返回还是抛出异常 , 后置通知都会执行。如果只想在连接点返回的时候记录日志 , 应使用返回通知代替后置通知。

 package com.itdoc.spring.aop.circular;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component; /**
* 通知
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-04 9:50
*/
@Aspect
@Component
public class AsjectLogging { /**
* 在方法正常结束后执行代码。
* 返回通知是可以访问到方法的返回值的。
*/
@AfterReturning(value = "execution(* com.itdoc.spring.aop.circular.*.*(..))", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
Object methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends with " + result);
}
}

异常通知:

 package com.itdoc.spring.aop.circular;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component; /**
* 通知
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-04 9:50
*/
@Aspect
@Component
public class AsjectLogging { /**
* 在目标方法出现异常时候执行的代码。
* 可以访问到异常对象, 且可以指定在出现特定异常时再执行的代码。
*/
@AfterThrowing(value = "execution(* com.itdoc.spring.aop.circular.*.*(..))", throwing = "e")
public void afterThrowing(JoinPoint joinPoint, Exception e) {
Object methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " exception with " + e);
}
}

环绕通知:

 package com.itdoc.spring.aop.circular;

 import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component; import java.util.Arrays; /**
* 通知
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-04 9:50
*/
@Aspect
@Component
public class AsjectLogging { /**
* 环绕通知需携带 ProceedingJoinPoint 类型的参数。
* 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型参数可以决定是否执行目标方法。
* 环绕通知必须有返回值, 返回值即目标方法的返回值。
*
* @param point
* @return
*/
@Around("execution(* com.itdoc.spring.aop.circular.*.*(..))")
public Object around(ProceedingJoinPoint point) {
Object methodName = point.getSignature().getName();
Object[] args = point.getArgs();
Object result = null;
try {
//前置通知
System.out.println("The method " + methodName + " begins with" + Arrays.asList(args));
//执行方法
result = point.proceed();
//返回通知
System.out.println("The method " + methodName + " ends with " + result);
} catch (Throwable e) {
e.printStackTrace();
//异常通知
System.out.println("The method " + methodName + " exception with " + e);
} finally {
//后置通知
System.out.println("The method " + methodName + " ends");
}
return result;
}
}

Spring学习--用 ASpectJ 注解实现 AOP的更多相关文章

  1. spring学习笔记二 注解及AOP

    本节需要导入spring-aop包 注解 使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值. 扫描某个包下的所有类中的注解 <?xml version="1. ...

  2. Spring学习之Aspectj开发实现AOP

    Aspectj是一个基于Java语言的Aop框架,它提供了强大的Aop功能. Aspectj简介: 1.Aspectj是一个面向切面的框架,它扩展了Java语言,它定义了一个Aop语法. 2.所以它有 ...

  3. Spring学习笔记5——注解方式AOP

    第一步:注解配置业务类 使用@Component("Pservice")注解ProductService 类 package com.spring.service; import ...

  4. spring 学习(三):aop 学习

    spring 学习(三):aop 学习 aop 概念 1 aop:面向切面(方面)编程,扩展功能不修改源代码实现 2 AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码 3 aop底层使用动态代 ...

  5. spring-AOP框架(基于AspectJ注解配置AOP)

    基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...

  6. Spring学习4-面向切面(AOP)之Spring接口方式

    一.初识AOP    关于AOP的学习可以参看帮助文档:spring-3.2.0.M2\docs\reference\html目录下index.html的相关章节       1.AOP:Aspect ...

  7. Spring学习_day02_AOP,AspectJ,JdbcTemplate

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! Spring_day02 一.AOP面向切面编程 1. ...

  8. Spring学习笔记(12)——aop

    先了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个"时 ...

  9. Spring学习笔记-面向切面(AOP)-04

    什么是面向切面编程 先大概了解一下部分术语 横切关注点:软件开发中,散布于多出的功能称为横切关注点(cross-cutting concern),简单的可以描述为可以影响应用多处的功能,比如日志.安全 ...

随机推荐

  1. WPF中的数据模板(DataTemplate)

    原文:WPF中的数据模板(DataTemplate) WPF中的数据模板(DataTemplate)                                                   ...

  2. jmeter之Synchronizing Timer的理解

    该功能类似loadrunner的集合点,一般按照jmeter的树形结构,放在需要设置集合点的请求之前,两个参数的意思,我们先看官网的解释: 大概意思就是: Number of Simulated Us ...

  3. Qt Qwdget 汽车仪表知识点拆解6 自定义控件

    先贴上效果图,注意,没有写逻辑,都是乱动的 这里说一下控件自定义 图中标出的部分都是自定义的控件 这里如果我们有批量类似的功能,就可以使用自定义控件的方式,这里我已下面的自定义控件说一下,上面的在上一 ...

  4. jmeter之HTTP请求

    1.添加一个线程组:Test plan_添加_Threads(users)_线程组(右键操作),如下图: 2.添加一个HTTP请求:线程组_添加_sample_HTTP请求(右键操作),如下图: 3. ...

  5. 树莓派putty远程登录windows

    刚买树莓派的你,还在为要不要购买昂贵的屏幕而纠结吗?看完本博客学会远程登录,妈妈再也不用担心我的学习... 首先我们要知道树莓派的官方推荐系统是raspbian 很建议安装16年9月份的,其他的总是这 ...

  6. 测试理论-selenium的工作原理

  7. lintcode-111-爬楼梯

    111-爬楼梯 假设你正在爬楼梯,需要n步你才能到达顶部.但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? 样例 比如n=3,1+1+1=1+2=2+1=3,共有3中不同的方法 返回 3 ...

  8. python3 urllib和requests模块

    urllib模块是python自带的,直接调用就好,用法如下: 1 #处理get请求,不传data,则为get请求 2 import urllib 3 from urllib.request impo ...

  9. js把字符串格式的时间转换成几秒前、几分钟前、几小时前、几天前等格式

    最近在做项目的时候,需要把后台返回的时间转换成几秒前.几分钟前.几小时前.几天前等的格式:后台返回的时间格式为:2015-07-30 09:36:10,需要根据当前的时间与返回的时间进行对比,最后显示 ...

  10. ElasticSearch1.7.1拼音插件elasticsearch-analysis-pinyin-1.3.3使用介绍

    ElasticSearch拼音插件elasticsearch-analysis-pinyin使用介绍 https://my.oschina.net/xiaohui249/blog/214505 摘要: ...