经过了前面一章的学习大家基本理解了SpringAOP的简单工作原理,但是那只是最基本的一些操作,Spring的设计师们知道我们不想在诸如得到一个代理类、需要实现哪些接口 这些琐碎的事情上牵扯过多的经历,毕竟我们是中国软件产业的栋梁我们还要做更重要的事情。^_^  所以他们给我们准备了好多好东西,下面我就来介绍一下
拦截器接口MethodBeforeAdvice 所在包org.springframework.aop.MethodBeforeAdvice
功能:可以在调用的目标方法之前加入功能。方法:

void before(Method method, Object[] args, Object target)
拦截器接口AfterReturningAdvice  所在包org.springframework.aop.AfterReturningAdvice
功能:可以在调用的目标方法之后加入功能。方法:

void afterReturning(Object returnValue, Method method, Object[] args, Object target)
拦截器接口MethodInterceptor 所在包 org.aopalliance.intercept.MethodInterceptor
功能:可以在调用的目标方法前后(也可以叫周围)加入功能。方法:

 java.lang.Object invoke(MethodInvocation invocation)
接口MethodInvocation 所在包 org.aopalliance.intercept.MethodInvocation
MethodInvocation 接口从父接口Joinpoint处继承到一个方法

java.lang.Object proceed()
调用proceed方法方法用于执行目标类的目标方法。
下面我通过现实的代码来讲解Spring中AOP的使用方法,我还使用我以前例子中的接口Foo和实现此接口的类FooClass【开发环境为Eclipse】
便于大家阅读我将全部代码贴上来,首先是接口Foo,我在这个接口中增加了一个打印整数的方法
public interface Foo {
 public void printMessage(String message);
 public void printInteger(int num);
}
实现Foo接口的类FooClass
public class FooClass implements Foo {
public void printMessage(String message) {
  
  System.out.println(this.getClass().getName()+" "+message);
 }
 public void printInteger(int num)
 {
  System.out.println(this.getClass().getName()+" "+num);
 }
}
实现上面三个拦截器接口【AfterReturningAdvice,MethodBeforeAdvice,MethodInterceptor 】的类MyAdvice
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
public class MyAdvice implements AfterReturningAdvice,MethodBeforeAdvice,MethodInterceptor
{
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
   Object arg3) throws Throwable {
  Log log = LogFactory.getLog(MyAfterReturningAdvice.class);
  log.info("我在程序运行之后出现");
  
 }
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
  Log log = LogFactory.getLog(MyAfterReturningAdvice.class);
  log.info("我在程序运行之前出现");
  
 }
public Object invoke(MethodInvocation arg0) throws Throwable {
Log log = LogFactory.getLog(MyAfterReturningAdvice.class);
  log.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
  Object obj = arg0.proceed();
  log.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
  return obj;
 }
}
由于我使用的Junit测试,所以有一个Junit测试类AOPJunitTest
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
public class AOPJunitTest extends TestCase {
 ApplicationContext act = null; //使用的应用上下文类
 protected void setUp() throws Exception {
  super.setUp();
  this.act = new ClassPathXmlApplicationContext("/applicationContext.xml");//读取XML配置文件
  
 }
 public void testprintMessage()
 { 
   Foo foo = (Foo)act.getBean("proxy");//通过依赖注入得到FooClass对象的实例这个对象是Spring的一个类                                                                 org.springframework.aop.framework.ProxyFactoryBean这个类需要三个注入分别是
 void setTarget(Object target) 得到注入一个目标对象(真正要干活的类)
void setProxyInterfaces(String[] interfaceNames) 目标对象所实现的接口数组
void setInterceptorNames(String[] interceptorNames) 拦截器数组(因为我们有多种拦截器)

foo.printMessage("Hello!");
   foo.printInteger(20);
 }
}

下面是我们的XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "[url]http://www.springframework.org/dtd/spring-beans.dtd[/url]">
<beans>
 <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="target">
   <ref bean="fooClass"/>
  </property>
  
  <property name="proxyInterfaces">
   <value>Foo</value>
  </property>
  
  <property name="interceptorNames">
   <list>
    <value>interceptorReturning</value>
   </list>
  </property>
 </bean>
 
 
 <bean id="fooClass" class="FooClass"></bean>
 <bean id="interceptorReturning" class="MyAdvice"></bean>
  
 </beans>
程序运行结果
INFO - $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
INFO - 我在程序运行之前出现
FooClass Hello!
INFO - 我在程序运行之后出现
INFO - $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
INFO - $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
INFO - 我在程序运行之前出现
FooClass 20
INFO - 我在程序运行之后出现
INFO - $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

Spring从菜鸟到高手(二)AOP的真正实现相关推荐

  1. Spring从菜鸟到高手(四)(上)使用JdbcTemplate类实现用户登陆验证、批量更新

    标签:Spring java JdbcTemplate Spring从菜鸟到高手 绝缘材料 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.ht ...

  2. Spring从菜鸟到高手(一)实现AOP的基本原理

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://tonyaction.blog.51cto.com/227462/42039 Sp ...

  3. 从菜鸟到高手演变-----Java学习方法

    前段时间逛论坛,总会有很多新手很迷茫,问到:到底该怎么学好Java,这个问题很重要,尤其对于像我们这样大多数都是靠自学的人来说,找到一个好的学习方法至关重要!于是乎,我就给大家回答了很多,突然间想到了 ...

  4. Java之美[从菜鸟到高手演变]系列之博文阅读导航

    随着博文越来越多,为博客添加一个导航很有必要!本博客将相继开通Java.CloudFoundry.Linux.Ruby等专栏,都会设立目录,希望读者朋友们能更加方便的阅读! 在阅读的过程中有任何问题, ...

  5. 干货 | 大数据人工智能领域从菜鸟到高手晋级指南

    作者 | 王明哲.王存光 校对 | 丁楠雅 本文长度为5600字,建议阅读10分钟 本文为你剖析当下的时代背景,为在大数据江湖中修炼的行者提供升级建议. 我们身处一个"技术爆炸"和 ...

  6. VMware View 5.0从菜鸟到高手系列 10 –远程图形工作站配置篇

    本文档依照PCoIP设备供应商丽台(Leadtek)提供的零客户机VP200P以及PCoI卡VP200H为例,为了介绍整个安装步骤.以下配置在VMware view 4.5中安装,但在更新的版本中如V ...

  7. Python之美[从菜鸟到高手]--一步一步动手给Python写扩展(异常处理和引用计数)

    我们将继续一步一步动手给Python写扩展,通过上一篇我们学习了如何写扩展,本篇将介绍一些高级话题,如异常,引用计数问题等.强烈建议先看上一篇,Python之美[从菜鸟到高手]--一步一步动手给Pyt ...

  8. Python从菜鸟到高手(2):清空Python控制台

    执行python命令会进入Python控制台.在Python控制台中可以用交互的方式执行Python语句.也就是执行一行Python语句,会立刻返回执行结果.   当Python控制台输入过多的Pyt ...

  9. JavaEE开发之Spring中的依赖注入与AOP编程

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

最新文章

  1. Neural Tensor Network详细介绍
  2. 临时表temporary table
  3. 1013: C语言程序设计教程(第三版)课后习题6.3
  4. 逆序数2 HDOJ 1394 Minimum Inversion Number
  5. 【译】The Faults and Shortcomings of the EVM
  6. android -自定义view
  7. hive的错误编码+解决方案汇总(持续更新中)
  8. jwt重放攻击_【干货分享】基于JWT的Token认证机制及安全问题
  9. 【Es】es deep paging问题
  10. srs流媒体服务器windows_基于SRS构建的直播平台的监控系统的搭建思路与实现方法...
  11. 腾讯说:云上移动开发很简单(这不是P的)!
  12. Pyhton学习——Day60
  13. [渝粤教育] 西南科技大学 理论力学 在线考试复习资料(1)
  14. WPS企业免费版,无广告
  15. PHP WebSehll 后门脚本与检测工具
  16. mysql5.7架设征途服务器,征途服务端架设详细教程
  17. python:计算四分位距IQR
  18. 《剑指offer》序——面试流程及面试须知
  19. tp5 操作web3
  20. [Flask] [Python3] 第一个flask APP

热门文章

  1. 谷歌程序员犯低级错误?少打一个字符引发重大 Bug,致大量 Chromebook 无法解锁...
  2. 为什么 CTO、技术总监、架构师都不写代码,还这么牛?
  3. 天才黑客 Flanker 疑因拒绝做黑客被拼多多强行辞退
  4. 被微软称为 “世界的电脑” ,Azure 到底有多牛?
  5. 今天,我辞去了亚马逊年薪 50 万美金的工作!
  6. Linux学习笔记(十二)usermod、passwd、mkpasswd
  7. 说说设计模式~建造者模式(Builder)
  8. Swift教程之控制流
  9. WebApi接口安全认证——HTTP之摘要认证
  10. Linux下使用Opencv打开笔记本摄像头