Spring对AOP的实现提供了很好的支持。下面我们就使用Spring的注解来完成AOP做一个例子。

首先,为了使用Spring的AOP注解功能,必须导入如下几个包。aspectjrt.jar,aspectjweaver.jar,cglib-nodep.jar.

然后我们写一个接口

  1. package com.bird.service;
  2. public interface PersonServer {
  3. public void save(String name);
  4. public void update(String name, Integer id);
  5. public String getPersonName(Integer id);
  6. }

和一个接口实现类

  1. package com.bird.service.impl;
  2. import com.bird.service.PersonServer;
  3. public class PersonServiceBean implements PersonServer{
  4. @Override
  5. public void save(String name) {
  6. System.out.println("我是save方法");
  7. //  throw new RuntimeException();
  8. }
  9. @Override
  10. public void update(String name, Integer id) {
  11. System.out.println("我是update()方法");
  12. }
  13. @Override
  14. public String getPersonName(Integer id) {
  15. System.out.println("我是getPersonName()方法");
  16. return "xxx";
  17. }
  18. }

下面使用Spring注解方式对这个Bean进行方法拦截

  1. package com.bird.service;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.After;
  4. import org.aspectj.lang.annotation.AfterReturning;
  5. import org.aspectj.lang.annotation.AfterThrowing;
  6. import org.aspectj.lang.annotation.Around;
  7. import org.aspectj.lang.annotation.Aspect;
  8. import org.aspectj.lang.annotation.Before;
  9. import org.aspectj.lang.annotation.Pointcut;
  10. /**
  11. * 切面
  12. * @author Bird
  13. *
  14. */
  15. @Aspect
  16. public class MyInterceptor {
  17. @Pointcut("execution(* com.bird.service.impl.PersonServiceBean.*(..))")
  18. private void anyMethod(){}//定义一个切入点
  19. @Before("anyMethod() && args(name)")
  20. public void doAccessCheck(String name){
  21. System.out.println(name);
  22. System.out.println("前置通知");
  23. }
  24. @AfterReturning("anyMethod()")
  25. public void doAfter(){
  26. System.out.println("后置通知");
  27. }
  28. @After("anyMethod()")
  29. public void after(){
  30. System.out.println("最终通知");
  31. }
  32. @AfterThrowing("anyMethod()")
  33. public void doAfterThrow(){
  34. System.out.println("例外通知");
  35. }
  36. @Around("anyMethod()")
  37. public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
  38. System.out.println("进入环绕通知");
  39. Object object = pjp.proceed();//执行该方法
  40. System.out.println("退出方法");
  41. return object;
  42. }
  43. }
  1. @Pointcut("execution(* com.bird.service.impl.PersonServiceBean.*(..))")

这句话是方法切入点,execution为执行的意思,*代表任意返回值,然后是包名,.*意思是包下面的所有子包。(..)代

表各种方法.

然后下面的注解就比较简单了,就是在使用方法前和中,还有环绕拦截/

然后在Spring的配置文件中继续配置Bean,需要打开AOP命名空间

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/aop
  9. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  10. <aop:aspectj-autoproxy/>
  11. <bean id="personServiceBean" class="com.bird.service.impl.PersonServiceBean"/>
  12. <bean id="myInterceptor" class="com.bird.service.MyInterceptor"/>
  13. </beans>

然后建立一个Junit测试

  1. package junit.test;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import com.bird.service.PersonServer;
  6. public class SpringAOPTest {
  7. @Test
  8. public void inteceptorTest(){
  9. ApplicationContext ctx = new ClassPathXmlApplicationContext("beanAop.xml");
  10. PersonServer bean = (PersonServer)ctx.getBean("personServiceBean");
  11. bean.save(null);
  12. }
  13. }

测试结果为

    1. 2012-3-12 18:08:39 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    2. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@dd20f6: display name [org.springframework.context.support.ClassPathXmlApplicationContext@dd20f6]; startup date [Mon Mar 12 18:08:39 CST 2012]; root of context hierarchy
    3. 2012-3-12 18:08:40 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    4. 信息: Loading XML bean definitions from class path resource [beanAop.xml]
    5. 2012-3-12 18:08:40 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
    6. 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@dd20f6]: org.springframework.beans.factory.support.DefaultListableBeanFactory@b0bad7
    7. 2012-3-12 18:08:40 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    8. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@b0bad7: defining beans [org.springframework.aop.config.internalAutoProxyCreator,personServiceBean,myInterceptor]; root of factory hierarchy
    9. null
    10. 前置通知
    11. 进入环绕通知
    12. 我是save方法
    13. 后置通知
    14. 退出方法
    15. 最终通知

Spring的注解方式实现AOP的更多相关文章

  1. (转)使用Spring的注解方式实现AOP的细节

    http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...

  2. (转)使用Spring的注解方式实现AOP入门

    http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...

  3. 使用Spring的注解方式实现AOP

    Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...

  4. spring 纯注解方式 与AOP

    spring注解方式 以前我也使用过纯注解方式.现在在这里做个记录 我们先认识几个我们都耳熟能详的注解 @configuration :从spring3.0这个注解就可以用于定义配置类,可以替换xml ...

  5. Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop

    Spring系列之aop aop是什么?+xml方式实现aop+注解方式实现aop 什么是AOP? AOP为Aspect Oriented Programming 的缩写,意识为面向切面的编程,是通过 ...

  6. 基于AspectJ的注解方式进行AOP开发

    -------------------siwuxie095                                     基于 AspectJ 的注解方式进行 AOP 开发         ...

  7. 使用注解方式实现 AOP和IoC

    使用注解方式实现AOP和IoC IOC和DI的注解 IOC: @Component:实现Bean组件的定义 @Repository:用于标注DAO类,功能与@Component作用相当 @Servic ...

  8. mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类

    相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...

  9. spring事务管理方式,aop

    达内12 note unit 09 01 1.spring事务管理 2.spring提供了对事务管理支持 spring采用aop机制完成事务控制 可以实现在不修改原有组件代码情况下实现事务控制功能. ...

随机推荐

  1. 使用git

    原文出处: 涂根华的博客 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候 ...

  2. 论文阅读(Xiang Bai——【CVPR2012】Detecting Texts of Arbitrary Orientations in Natural Images)

    Xiang Bai--[CVPR2012]Detecting Texts of Arbitrary Orientations in Natural Images 目录 作者和相关链接 方法概括 方法细 ...

  3. Hadoop:操作 Hadoop Cluster

    启动Hadoop 当完成所有的必要配置后,将HADOOP_CONF_DIR目录中的所有配置文件复制到所有机器,建议将HDFS和YARN后台进程一不同的用户身份运行,比如运行HDFS进程们的用户为hdf ...

  4. mysql 性能优化方案1

    网 上有不少mysql 性能优化方案,不过,mysql的优化同sql server相比,更为麻烦与复杂,同样的设置,在不同的环境下 ,由于内存,访问量,读写频率,数据差异等等情况,可能会出现不同的结果 ...

  5. 用node.js给图片加水印

    一.准备工作: 首先,确保你本地已经安装好了node环境.然后,我们进行图像编辑操作需要用到一个Node.js的库:images.这个库的地址是:https://github.com/zhangyua ...

  6. 仿网易新闻 ViewPager 实现图片自动轮播

    新闻 App 首页最上方一般会循环播放热点图片,如下图所示. 本文主要介绍了利用 ViewPager 实现轮播图片,图片下方加上小圆点指示器标记当前位置,并利用 Timer+Handler 实现了自动 ...

  7. 整理单机安装RabbitMQ,留着备用

    一.安装erlang 1.安装erlang依赖: yum install build-essential openssl openssl-devel unixODBC unixODBC-devel m ...

  8. MVC系列2-Model

    上一篇我讲了ASP.MET MVC的基础概念,我相信从上一篇,我们可以知道MVC的执行过程.这一篇我们开始讲解Model.我们知道,在我们的应用程序中,大多时候是在遵循业务逻辑通过UI操作数据.所以这 ...

  9. mysql中正则表达式的使用

    mysql中正则表达式的性能要高于like,所以这里总结一下正则表达式的使用. 正则表达式的模式及其含义: 下面举例说明其用法: 建表student: create table student(id ...

  10. jQuery瀑布流插件——jQuery.Waterfall

    插件--jQuery.Waterfall 思路: 其实只要了解了整个流程,要实现这个插件也不难,大家都玩过俄罗斯方块吧,原理差不多,找到合适的地方叠上去就好了,在这里,每个块的宽度是必需给定的,然后计 ...