1.前置通知

接口:

package chapter1.server;public interface IHelloService {public void sayAdvisorBefore(String param) ;
}

  实现

package chapter1.service.impl;import chapter1.server.IHelloService;public class HelloService implements IHelloService {   public void sayAdvisorBefore(String param) {// TODO Auto-generated method stubSystem.out.println("============say " + param);}}

  配置:

<!-- 启动对Aspectj的支持 -->
<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService" />
<bean id="aspect" class="chapter1.aop.HelloAspect"/>

  aop:

package chapter1.aop;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;@Aspect
public class HelloAspect {//定义切入点@Pointcut(value="execution(* chapter1..*.sayAdvisorBefore(java.lang.String)) && args(param)", argNames = "param")public void beforePointcut(String param) {}//定义通知@Before(value = "beforePointcut(param)", argNames = "param")public void beforeAdvice(String param) {System.out.println("===========before advice param:" + param);}
}

  测试程序:

//前置通知public void testAspectj(){ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj.xml");IHelloService hello = context.getBean("helloService", IHelloService.class);hello.sayAdvisorBefore("before");}

  

2.后置返回通知

接口

package chapter1.server;public interface IHelloService2 {public int sayAfterReturning(String param);
}

  实现

package chapter1.service.impl;import chapter1.server.IHelloService2;public class HelloService2 implements IHelloService2 {public int sayAfterReturning(String param) {// TODO Auto-generated method stubSystem.out.println("============ say after returning:" + param);return 1;}}

  配置:

<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService2" />
<bean id="aspect" class="chapter1.aop.HelloAspect2"/>

  aop:

package chapter1.aop;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.AfterReturning;@Aspect
public class HelloAspect2 {//方法一//通知@AfterReturning(   //value="execution(* chapter1..*.sayAdvisorBefore(java.lang.String)) and args(param)",value="execution(* chapter1..*.sayAfterReturning(..))",         argNames="retVal", returning="retVal")public void afterReturningAdvice(Object retVal){System.out.println("================= return after advice : " + retVal);}//方法二//定义切入点@Pointcut(value="execution(* chapter1..*.sayAfterReturning(java.lang.String) and args(param))", argNames="param")public void returnPointcut(String param) {}public void afterReturningAdvice2(Object retVal){}}

  测试程序:

//后置返回通知public void testAspectAfterReturning(){ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj2.xml");IHelloService2 hello = context.getBean("helloService", IHelloService2.class);hello.sayAfterReturning("hahah");}

  

3.后置错误通知

接口

package chapter1.server;public interface IHelloService3 {public boolean sayAfterThrow(String param);
}

  实现:

package chapter1.service.impl;import chapter1.server.IHelloService3;public class HelloService3 implements IHelloService3 {public boolean sayAfterThrow(String param) {// TODO Auto-generated method stubSystem.out.println("=========say after throw:" + param);throw new RuntimeException(); }}

  配置:

<!-- 开启对Aspectj的支持 -->
<aop:aspectj-autoproxy />
<bean id="helloService" class="chapter1.service.impl.HelloService3" />
<bean id="aspect" class="chapter1.aop.HelloAspect3" />

  aop:

package chapter1.aop;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterThrowing;@Aspect
public class HelloAspect3 {@AfterThrowing(value="execution(* chapter1..*.sayAfterThrow(java.lang.String))", argNames="exception", throwing="exception")public void afterThrowAdvice(Exception exception){System.out.println("=========after throwing advice : " + exception);}
}

  测试程序:

//后置错误通知    public void testAfterThrow(){ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj3.xml");IHelloService3 hello = context.getBean("helloService", IHelloService3.class);hello.sayAfterThrow("error");}

  

4.环绕通知

接口:

package chapter1.server;public interface IHelloService4 {public void sayAround(String param);
}

  实现:

package chapter1.service.impl;import chapter1.server.IHelloService4;public class HelloService4 implements IHelloService4 {public void sayAround(String param) {// TODO Auto-generated method stubSystem.out.println("============= say around: " + param);}}

  配置:

<!-- 开启对Aspectj的支持 -->
<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService4" />
<bean id="aspect" class="chapter1.aop.HelloAspect4"/>

  aop:

package chapter1.aop;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;@Aspect
public class HelloAspect4 {@Around(value="execution(* chapter1..*.sayAround(java.lang.String))", argNames="param")public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable{System.out.println("========= around before advice");Object retVal = pjp.proceed(new Object[] {"我被替换了呀"});System.out.println("========= around before advice");return retVal;}
}

  测试程序:

//环绕通知  public void testAround(){ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj4.xml");IHelloService4 hello = context.getBean("helloService", IHelloService4.class);hello.sayAround("gaga ya xia ba");}

  

5.引入(结合chatper1.service.IHelloService程序来试验)

接口:

package chapter1.server;public interface IHelloService5 {public void sayDeclare();
}

  实现:

package chapter1.service.impl;import chapter1.server.IHelloService5;public class HelloService5 implements IHelloService5 {public void sayDeclare() {// TODO Auto-generated method stubSystem.out.println("=========== say declare  " );  }}

  配置:

<!-- 开启对Aspect的支持 -->
<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService"/>
<bean id="aspect" class="chapter1.aop.HelloAspect5"/>

  aop:

package chapter1.aop;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;import chapter1.server.IHelloService5;
import chapter1.service.impl.HelloService5;@Aspect
public class HelloAspect5 {@DeclareParents(value="chapter1..*.HelloService+",defaultImpl=HelloService5.class)public IHelloService5 ihelloService5;}

  测试程序:

//引入@Testpublic void testDeclare(){ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj5.xml");IHelloService5 hello = context.getBean("helloService", IHelloService5.class);hello.sayDeclare();}

  

转载于:https://www.cnblogs.com/achengmu/p/8580207.html

spring3: 切面及通知实例 Aspectj的aop相关推荐

  1. Spring使用AspectJ开发AOP

    AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...

  2. AOP中的切点、切面、通知等

    在AOP中,切点.切面和通知是三个核心概念,下面分别进行介绍. 切点(Pointcut) 切点是一个表达式,用于描述哪些类的哪些方法会被拦截.通常情况下,切点会使用表达式语言(如AspectJ)来定义 ...

  3. 实例简述Spring AOP之对AspectJ语法的支持

    Spring的AOP可以通过对@AspectJ注解的支持和在XML中配置来实现,本文通过实例简述如何在Spring中使用AspectJ. 一:使用AspectJ注解: 1,启用对AspectJ的支持: ...

  4. AOP系列:切面编程框架 : AspectJ

    Android AOP方案(一)--AspectJ - 掘金AOP是一个概念,并没有设定具体语言的实现,它能克服那些只有单继承特性语言的缺点(如Java),AOP具体实现有很多种方式,AspectJ ...

  5. AspectJ对AOP的实现

    引言 1.对于AOP编程思想,很多框架都进行了实现,Spring就是其中之一,可以完成面向切面编程.然后,AspectJ也实现了AOP的功能.并且实现的更加简单.方便,并且还支持注解式开发.所以,sp ...

  6. Spring AOP编程-传统基于aspectJ切点AOP开发

    1.在配置文件上方增加aop相关配置. 2.在spring的配置文件中定义目标与通知. 3.使用aop:xxx标签来完成切面与切点声明. 4.我们使用aspectj的切面声明方式 需要在导入aspec ...

  7. 使用AspectJ开发AOP更加便捷,你不知道嘛

    前文 中,已经讲解了Spring传统的AOP开发,但在实际开发中,我们都是使用AspectJ进行AOP开发. AspectJ 简介 AspectJ 是一个基于Java语言的独立的AOP框架. 在没有A ...

  8. (转)Spring使用AspectJ进行AOP的开发:注解方式

    http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Sprin ...

  9. Spirng使用Aspectj实现AOP

    Aspectj实现AOP有两种方式: (1)基于aspectj的xml配置: (2)基于aspectj的注解方式: 一.基于aspectj的xml配置: 1.导入相关的AOP的jar包: 2.创建Sp ...

最新文章

  1. 串联NSOperation知识点
  2. jQuery用于请求服务器的函数
  3. 深度学习之自编码器AutoEncoder
  4. The working copy is locked due to a previous error.
  5. 一个基于用户的API限流策略 Rate Limit
  6. 我们要在离职时,优雅地说再见!
  7. 牛客小白月赛13-H(单调栈+树状数组)
  8. oracle 迁库 教程,Oracle整库文件迁移步骤详解教程
  9. 一位全减器逻辑电路图_用74ls138实现一位全减器 - 数字电路图
  10. 【180620】小人物走路、奔跑的VC++游戏特效
  11. 实验一 Linux基本操作
  12. UNICODE与 UTF-8的转换详解
  13. 数字中国城市巡礼之开封:千年古都的智慧新生
  14. unity3d 获取 Advertising ID
  15. web项目bug总结
  16. 判断奇数还是偶数【c语言】
  17. 手机百度浏览器ua标识在哪里_荣耀play浏览器ua标识在哪里
  18. C语言 | Linux下的静态链接与动态链接
  19. Sentinel Dubbo 适配器看限流与熔断(实战思考篇)
  20. php大商创 安装,新零售电商系统:大商创X安装教程【宝塔环境】

热门文章

  1. java语言中json转换,JSON字符串和JAVA语言对象的相互转换教程
  2. java new对象 =null_在Java中将对象分配为null会影响垃圾回收吗?
  3. Java常用设计模式————享元模式
  4. spring源码分析第一天------源码分析知识储备
  5. 检索有关计算机系统功能设计方面的文献,文献检索系统
  6. 计算机科学陈国华,科学网—模式识别与智能系统是个什么专业? - 晏燕华的博文...
  7. php进程数是指什么,25.查看php 某个服务的进程数
  8. arm-linux-gcc 硬浮点,ARMCC和GCC编译ARM代码的软浮点和硬浮点问题 【转】
  9. HTML+CSS+JS实现 ❤️美女拼图游戏❤️
  10. HTML+CSS+JS实现 ❤️H5 3D传送带视差照片特效❤️