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. Android--带你一点点封装项目 MVP+BaseActivity+Retrofit+Dagger+RxJava(一)

    1,其实早就想把这些东西给封装封装的,一直没有时间,今天刚好项目进入到测试阶段了,Bug同事在哪儿测试的飞起,但发现提bug的尽然是我(得意脸),然后上午把ios的包测试了一下,顺便把服务器给测挂了( ...

  2. java异常处理预习

    一.什么是异常处理 在程序运行过程中发生的.会打断程序正常执行的事件称为异常(Exception),也称为例外.比如:除零溢出.数组越界.内存溢出等,这些事件的发生将阻止程序的正常运行.传统的编程语言 ...

  3. apiCloud又拍云数据库操作经验

    1.在数据库表中有个relation字段,表示一对多(对应其他表的多个记录),这个字段比较奇怪,因为实际上这个字段里并未保存实际值.而是保存在对应表的字段中.但是对应表显示出来并没有这个字段.导出数据 ...

  4. PhpStorm创建Drupal模块项目开发教程(1)

    用户体验是应用程序开发的一个重要因素.一个加载时间长.设计差.导航不清晰的网站很难留住客户. Web应用程序由多个部分组成,并且可能运行在多种环境中,所以Web应用程序从开发开始就需要在所有环境中进行 ...

  5. IOS 类似微博,#话题#,@人,[表情] 网址 正则匹配

    /** *获取需要处理的子字符串和子串的range */ -(NSArray<NSTextCheckingResult *> *)getBBSLetterSubStrRangeArrWit ...

  6. J2EE开发时的包命名规则

    http://www.blogjava.net/paulwong/archive/2012/04/15/374675.html 转一个J2EE开发时的包命名规则,养成良好的开发习惯 代码编写规范目的: ...

  7. [java报错]Could not instantiate listener XXXXXX

    写在开头的话: 兜兜转转,辞去了深圳的工作,回到了武汉,从事的居然一度是我最不想学的语言-java,曾经以为自己并不会java,但是上手之后,发现语言都是相通的,自己一度排斥学习java真的是不能再傻 ...

  8. Java 自定义客户端与服务器

    一:浏览器如何请求数据基本原理 基本原理: 当浏览器输入地址向服务器请求数据时,实际上浏览器会在内部建立一个Socket对象,把http请求报文转变成byet[]字节,然后调用Socket的方法把这些 ...

  9. smarty模板引擎

    1.    使用smarty 1.1 项目引入 // 3, 连接数据库,提取相关数据 $title = "Smarty模板引擎"; $content = "Smarty模 ...

  10. Mac下体验Hexo与Github Pages搭建

    很久之前就知道Github可以发布自己的博客,因为仅支持静态文件,一直懒于动手自己进行编辑.再了解到还存在 JekyII 和 Hexo 这样的内容生成框架后,终于决定体验一下. 一.本地环境准备. G ...