`
qepipnu
  • 浏览: 74805 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Spring AOP之Hello World

阅读更多
我们使用一个简单的例子来演示一下Spring中的AOP,这是一个log的例子,实际上log是一个对于AOP来说很不好的例子,这里我们只为说明Spring AOP的使用。
  
  1.首先我们来创建一个自己的interceptor
  这个类必须继承org.aopalliance.intercept. MethodInterceptor接口。Spring的AOP框架就是参照aopalliance这个标准实现的,所以我们的MyInterceptor要继承这个标准中的接口。
  这个接口只有一个要求实现的方法:
  public Object invoke(MethodInvocation methodInvocation) throws Throwable;
  下面是我们的MyIntercptor:
  
  public class MyInterceptor implements MethodInterceptor {
  private final Log logger = LogFactory.getLog(getClass());
  
  public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  logger.info("Beginning method (1): " +
  methodInvocation.getMethod().getDeclaringClass() + "." +
  methodInvocation.getMethod().getName() + "()");
  long startTime = System.currentTimeMillis();
  try{
  Object result = methodInvocation.proceed();
  return result;
  }finally{
  logger.info("Ending method (1): " +
  methodInvocation.getMethod().getDeclaringClass() + "." +
  methodInvocation.getMethod().getName() + "()");
  logger.info("Method invocation time (1): " +
  (System.currentTimeMillis() - startTime) + " ms.");
  }
  }
  }
  
  对于上面的代码需要说明的是下面两行代码:
  Object result = methodInvocation.proceed();
  return result;
  整个程序的流程是这样的:
  1,先是执行在Object result = methodInvocation.proceed();前面的代码;
  2,接着执行Object result = methodInvocation.proceed();,它把执行控制权交给了interceptor stack(拦截器栈)内的下一个interceptor,如果没有了就交给真正的业务方法;
  3,然后执行return result;之前的代码;
  4,最后执行return result;,它把控制权交回它之上的interceptor,如果没有了就退出interceptor stack。
  
  2.写出我们的业务对象及其接口
  为了方便我们的业务接口只有一个hello方法:
  
  public interface BusinessInterface {
  public void hello();
  }
  
  业务对象的代码如下:
  
  public class BusinessInterfaceImpl implements BusinessInterface{
  public void hello() {
  System.out.println("hello Spring AOP.");
  }
  }
  
  3.接下来,我们来看看如何使用我们的写的interceptor
  我们把业务对象作为AOP的target:
  <bean id="businessTarget" class="com.rst.spring.testaop.BusinessInterfaceImpl"/>
  接着在bean定义中声明interceptor:
  <bean id="myInterceptor" class="com.rst.spring.testaop.MyInterceptor"/>
  最后,我们来声明真正的业务对象,通过使用它的接口以及Spring的ProxyFactoryBean:
  
  <bean id="businessBean"
    class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="proxyInterfaces">
  <value>com.rst.spring.testaop.BusinessInterface</value>
  </property>
  <property name="interceptorNames">
  <list>
  <value>myInterceptor</value>
  <value>businessTarget</value>
  </list>
  </property>
  </bean>
  
  这里需要说明两点:
  proxyInterfaces:就是我们的业务对象的实际接口;
  interceptorNames:定义了所有interceptors的执行顺序,其中业务对象的target作为list的最后一个。记着一定要把业务对象的target放到list中,否则你的业务对象就不会工作。
  
  4.最后,写我们的测试类
  ClassPathResource resource =
  new ClassPathResource("com/rst/spring/testaop/aop_bean.xml");
  XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
  BusinessInterface businessBean =
  (BusinessInterface) beanFactory.getBean("businessBean");
  businessBean.hello();
  
  一切正常就可以在log上看到相应的信息了。
  以下是附件源代码的执行效果:
  2004-09-08 16:04:51,210 INFO - Beginning method (1): interface com.rst.spring.testaop.BusinessInterface.hello()
  2004-09-08 16:04:51,210 INFO - Beginning method (2): interface com.rst.spring.testaop.BusinessInterface.hello()
  hello Spring AOP.
  2004-09-08 16:04:51,210 INFO - Ending method (2): interface com.rst.spring.testaop.BusinessInterface.hello()
  2004-09-08 16:04:51,210 INFO - Ending method (1): interface com.rst.spring.testaop.BusinessInterface.hello()
  2004-09-08 16:04:51,210 INFO - Method invocation time (1): 0 ms.
  源代码需要spring.jar, aopallience.jar, commons-logging.jar。

分享到:
评论

相关推荐

    Spring的AOP示例DEMO HELLOWORLD

    根据学习笔记整理的HelloWorld,需要自行下载Spring3相关的包

    SpringMVC3.1.2 入门级HelloWorld源码

    [INFO] | \- org.springframework:spring-aop:jar:3.1.2.RELEASE:compile [INFO] +- org.springframework:spring-core:jar:3.1.2.RELEASE:compile [INFO] | +- org.springframework:spring-asm:jar:3.1.2.RELEASE:...

    Spring的Hello World:理解AOP

    NULL 博文链接:https://istone.iteye.com/blog/423895

    hello Spring

    下面是Spring的HelloWorld的程序的文件结构: C:. │ .classpath │ .project │ ├─build │ └─classes │ └─com │ ├─dineshonjava │ │ └─sdnext │ │ └─springConfig │ │ spring.xml │ │ │ ...

    跟我学spring3(1-7)

    【第六章】 AOP 之 6.2 AOP的HelloWorld ——跟我学spring3 【第六章】 AOP 之 6.3 基于Schema的AOP ——跟我学spring3 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3 【第六章】 AOP 之 6.5 AspectJ...

    跟我学spring3(1-7).pdf

    —— 5.1 概述 5.2 SpEL基础5.3 SpEL语法5.4在Bean定义中使用EL6.1 AOP基础6.2 AOP的HelloWorld6.3 基于Schema的AOP6.4 基于@AspectJ的AOP 6.5 AspectJ切入点语法详解6.6 通知参数6.7 通知顺序6.8 切面实例化模型

    Java Framework 关于IOC、AOP、Log的案例源码

    该源码是课程 Java Spring案例精讲 ---- Spring框架 的源码,包含Java Spring的最简单的Hello World、IOC、AOP及Log的源码 Spring整体框架中的核心功能,例如:IOC、AOP、Bean生命周期、上下文、作用域、资源处理等...

    SSH(Struts1.0+Spring+Hibernate)框架集成笔记

    SSH框架集成是较复杂和难理解的,只有在不断的练习和使用中才能慢慢的理解其中的原理,仅凭看视频是远远不够的,因为这些涉及了尤其是spring底层的好多类以及控制翻转(IOC)和面向切面(AOP)编程的思想,不过在讲述...

    Spring入门笔记.md

    ## Spring入门学习 首先认识下Spring的结构 ![架构图]...&lt;bean id="helloBean" class="mybatis.study.start.bean.HelloWorld"&gt; ``` list Map,provincecitymysqq

    JBoss ESB 学习笔记

    12——第十一个ESB代码Spring Hello World 106 13——第十二个ESB代码Spring AOP 113 14——第十三个ESB代码Transform CSV to XML 122 15——第十四个ESB代码Transform XML to POJO 128 16——第十五个ESB代码Web ...

    跟开涛学Spring

    1.19 【第六章】 AOP 之 6.2 AOP的HelloWorld ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . .208 http://jinnianshilongnian.iteye.com 第 2 / 366 页 1.20 【第六章】 AOP 之 6.3 基于...

    helloworld.zip

    资源概要: springboot项目中怎么使用aop全局拦截controller的url以及参数;拦截器的使用等示例demo;springbootAop的应用可以使开发者很...可以学到spring中关于aop切面的示例。以及熟练掌握aop在项目实战中的运用。

    spring学习笔记

    spring从HelloWorld到ioc,aop,对JDBC,hibernate,struts1,struts2的支持笔记

    SpringFramework中的AOP编程之入门篇

    使用跟踪和记录方面(面向方面领域的HelloWorld)作为例子,本文展示了如何使用Spring框架所独有的特性来声明切入点和通知以便应用方面。本系列的第二部分将更深入地介绍如何运用Spring中的所有通知类型和切入点来...

    spring-boot示例项目

    该项目包含helloworld(快速入门)、web(ssh项目快速搭建)、aop(切面编程)、data-redis(redis缓存)、quartz(集群任务实现)、shiro(权限管理)、oauth2(四种认证模式)、shign(接口参数防篡改重放)、encoder(用户...

    Spring-Reference_zh_CN(Spring中文参考手册)

    使用BeanPostProcessor的Hello World示例 3.7.1.2. RequiredAnnotationBeanPostProcessor示例 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.2.1. PropertyPlaceholderConfigurer示例 3.7.2.2. ...

    SpringFramework中的面向方面编程(AOP),第二部分

    火龙果软件工程技术中心 在本系列的第一部分,我介绍了如何实现面向方面领域的“HelloWorld”:跟踪和记录方面。利用Spring框架所提供的面向方面编程(Aspect-OrientedProgramming,AOP)功能,您看到了如何使用...

Global site tag (gtag.js) - Google Analytics