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. 突袭HTML5之SVG 2D入门1 - SVG综述////////////////zzzzzzzz

    以二次贝塞尔曲线的公式为例: js函数: //p0.p1.p2三个点,其中p0为起点,p2为终点,p1为控制点 //它们的坐标用数组表示[x,y] //t的范围是0-1 function qBerzi ...

  2. javaSE第三篇

    1.classpath: set classpath = 类路径 用来找class文件 set classpath = .;(默认表示当前路径   2.数组的拷贝 方法一:Arrays.copyOf( ...

  3. 使用pip安装 cx_Oracle 模块

    1. 系统环境,linux, python3.5, pip以下命令的执行均使用root用户. 2. 执行 pip install cx_Oracle出错,提示不能定位Oracle的安装.出现该问题的原 ...

  4. EF并非我们想象的那么智能

    我之前在项目中用EF读取一个视图的数据,页面展示出现重复数据,当时百思不得其解,跟踪代码,数据读取时取到的数据并不是重复,为什么在前台显示就有重复了呢,我当时就在业务层将数据去重,但取到的数据跟数据库 ...

  5. js 一个自写的 监测类

    自从认识了jQuery后,很多页面加载入口,都放在document.ready里面.但是有时候这个觉得ready加载太慢, 这个[监测类 ]就开始产生了 效果类似这个. 每10毫秒检查一次,直到加载了 ...

  6. String空值判定

    不能用 str.equalsIgnoreCase(null). 写了个判定的方法,供以后直接调用: public static boolean isEmpty(String str) { if (st ...

  7. PostgreSQL入门

    这篇文章主要介绍我在最近工作中接触到的数据库PostgreSQL,做个入门记录,以便自己日后回顾. 1.下载安装,安装配置过程可以参考http://blog.chinaunix.net/uid-354 ...

  8. WCF、MongoDB

    http://www.cnblogs.com/quietwalk/archive/2011/08/09/2132573.html http://www.cnblogs.com/huangxinchen ...

  9. C#:获取环境信息

    外部环境数据1.需要管理员权限2.需要安装office2003以上完整版3.需要安装flash reader 10.0以上4.需要安装adodb reader;Adobe Acrobat X Pro; ...

  10. flag+文件操作

    flag标志位,标识位,在其他语言中可能叫开关,个人觉得当作开关更容易理解.下面我们来利用这个开关来控制文件操作的流程,从而优雅的修改配置文件. global log 127.0.0.1 local2 ...