Spring AOP 的@Aspect

 

转自:http://blog.csdn.net/tanghw/article/details/3862987

从Spring 2.0开始,可以使用基于schema及@AspectJ的方式来实现AOP,本文以一个简单的实例介绍了如何以@AspectJ方式在Spring中实现AOP。由于@Aspect是基于注解的,因此要求支持注解的5.0版本以上的JDK。

环境要求:     1. Web应用     2. 有一个专门提供系统服务的Service层

我们的目标是,如果用户调用Service层中任一方法,都在其插入一个记录信息的功能。

1. 一个最简单的AOP

共有2步。

1.1 定义一个Aspect

1.  package com.sarkuya.aop.aspect;     2.  import org.aspectj.lang.annotation.Aspect;     3.  import org.aspectj.lang.annotation.Before;     4.  @Aspect     5.  public class SampleAspect {     6.      @Before("execution(* com.sarkuya.service..*.*(..))")     7.      public void doBeforeInServiceLayer() {     8.          System.out.println("=====================================");     9.          System.out.println("Aop: do before in Service layer");     10.         System.out.println("=====================================");     11.   }     12. }

第4行,必须使用@Aspect在类名之前注解。

第6行,当用户调用com.sarkuya.service包中任一类的任一方法,在调用前,Spring将自动执行下面的doBeforeInServiceLayer()方法,此方法只是简单地打印一些信息。

1.2 在Spring配置文件applicationContext.xml中配置

<beans xmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"         xsi:schemaLocation="             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<aop:aspectj-autoproxy />         <bean class="com.sarkuya.aop.aspect.SampleAspect" />

<!-- ================ YOUR CONTENTS GOES BELOW =================== -->     </bean>

就这么简单。

2. 将Pointcut及Advice分开

上面的Aspect中混杂了Pointcut及Advice,因此最好将其分开。共有3步。

2.1 定义Pointcut

1.  package com.sarkuya.aop.aspect;     2.  import org.aspectj.lang.annotation.Aspect;     3.  import org.aspectj.lang.annotation.Pointcut;     4.  @Aspect     5.  public class SampleAspect {     6.      @Pointcut("execution(* com.sarkuya.service..*.*(..))")     7.      public void inServiceLayer() {     8.      }     9.  }

Pointcut是植入Advice的触发条件。每个Pointcut的定义包括2部分,一是表达式,如第6行;二是方法签名,如第7行。方法签名必须是 public及void型。可以将Pointcut中的方法看作是一个被Advice引用的助记符,因为表达式不直观,因此我们可以通过方法签名的方式为此表达式命名。因此Pointcut中的方法只需要方法签名,而不需要在方法体内编写实际代码。

2.2 定义Advice

1.  package com.sarkuya.aop.advice;     2.  import org.aspectj.lang.annotation.Aspect;     3.  import org.aspectj.lang.annotation.Before;     4.  @Aspect     5.  public class SampleAdvice {     6.      @Before("com.sarkuya.aop.aspect.SampleAspect.inServiceLayer()")     7.      public void logInfo() {     8.          System.out.println("=====================================");     9.          System.out.println("Aop: do before in service layer");     10.         System.out.println("=====================================");     11.     }     12. }

第4行,对于Advice,也只能使用@Aspect来注解。

第6行,与第1.1节中第6行不同,这次不是直接使用Pointcut的表达式,而是使用了Pointcut中的方法签名。

单独定义Pointcut的好处是,一是通过使用有意义的方法名,而不是难读的Pointcut表达式,使代码更加直观;二是Pointcut可以实现共享,被多个Advice直接调用。若有多个Advice调用某个Pointcut,而这个Pointcut的表达式在将来有改变时,只需修改一个地方,维护更加方便。

第7行,我们将Advice的方法法改为logInfo(),以更加明确此Advice的作用。

2.3 配置文件

<aop:aspectj-autoproxy />     <bean class="com.sarkuya.aop.advice.SampleAdvice" />

只需配置SampleAdvice,无需配置SampleAspect。

3. 重构:明确Pointcut职责

对于SampleAspect来说,其主要职责是定义Pointcut,可以在此类中同时定义多个Pointcuts。但其类名反映不出这个特点,因此,应将其重构以明确其职责。

package com.sarkuya.aop.pointcut;     import org.aspectj.lang.annotation.Aspect;     import org.aspectj.lang.annotation.Pointcut;     @Aspect     public class PointcutsDefinition {         @Pointcut("execution(* com.sarkuya.service..*.*(..))")         public void inServiceLayer() {         }     }

将SampleAspect重命名为PointcutsDefinition,并移到com.sarkuya.aop.pointcut包中。

对于SampleAdvice来说,只需改变@Before()的注解,指向     @Before("com.sarkuya.aop.pointcut.PointcutsDefinition.inServiceLayer()")

而Spring配置文件保持不变。

小结:     我们先从一个最简单的Aspect实例开始,了解AOP的作用及最基本的要求,再重构为更有意义的代码,明确了AOP中的Pointcut及Advice的概念,有助于我们构建更复杂的Aspect。

Spring AOP 的@Aspect的更多相关文章

  1. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  2. spring aop中aspect和advisor的区别

    之前看到spring AOP配置aspect(切面)有两种方式,一种是利用注解的方式配置,一种是利用XML的方式配置. 我们的配置是这样的<aop:aspect>,还有另外一种<ao ...

  3. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置(转)

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  4. 关于 Spring AOP (AspectJ) 该知晓的一切

    关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上 ...

  5. Spring AOP两种实现方式

    一. AOP 概念: Spring AOP 即Aspect Oriented Programming(面向切面编程), 实现方式分为两种: 1. 注解(Annotation) 2. 配置(Config ...

  6. spring aop两种配置方式

    基于注解的Spring AOP开发 简单案例快速入门 定义目标类接口和实现类 /** * Created by zejian on 2017/2/19.*/ //接口类 public interfac ...

  7. 基于Spring AOP实现的权限控制

    1.AOP简介 AOP,面向切面编程,往往被定义为促使软件系统实现关注点的分离的技术.系统是由许多不同的组件所组成的,每一个组件负责一块特定的功能.除了实现自身核心功能之外,这些组件还经常承担着额外的 ...

  8. 关于 Spring AOP (AspectJ) 你该知晓的一切

    版权声明:本文为CSDN博主「zejian_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/javazej ...

  9. 【Spring AOP】Spring AOP的使用方式【Q】

    Spring AOP的三种使用方式 经典AOP使用方式 改进XML配置方式 基于注解的方式 第1种方式可以作为理解spring配置AOP的基础,是最原始的配置方式,也体现了spring处理的过程. 使 ...

随机推荐

  1. $\mathcal{Miemeng}$的病态码风计划

    晚上困的要命,先写个码风计划提提神. 计划目标 抵制无理压行. 抵制不可读代码. 倡导代码艺术化,分层化 具体的一些细节和展示 1>整体 首先要把预读部分(我这么叫的),命名域使用,全局变量定义 ...

  2. Cf序列化器-Serializer解析

    Cf序列化器-Serializer 定义序列化器 Django REST framework中的Serializer使用类来定义,须继承自rest_framework.serializers.Seri ...

  3. Nginx负载均衡反向代理

    http{ upstream test.com { server 118.118.66.88:8080; } server { listen 80; server_name www.test.com; ...

  4. 凸优化 & 1概念

    ---恢复内容开始--- 放射集合 系数之和为1 相加仍然能在集合内,就是 纺射集合 子空间加一个常熟 就是纺射集合 , 例题2.1 一类特殊的线性方程组的解可以看作纺射 集合 纺射包 aff C 是 ...

  5. Oracle中给表主键、外键收集

    1.创建表的同时创建主键约束 (1)无命名 create table student ( studentid int primary key not null, studentname varchar ...

  6. Win7x64易语言调试进程无法退出

    这是个历史问题,几乎所有的Win7x64机器上都会碰到这个问题 解决方法: 启动黑月重新编译器

  7. Leetcode633.Sum of Square Numbers平方数之和

    给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2: 输入: 3 ...

  8. 转:解决Onethink上传视频的问题 超棒的解决方案

    用过Onethink的都知道,它是不能上传视频的. 有人想到用上传附件的方式上传视频,但是结果……就是提示没有上传文件. 要是正常上传个一两兆的图片啊,压缩文件什么的还是可以的. 所以,重点来了 怎么 ...

  9. pip list报错:DEPRECATION: The default format will switch to columns in the future.

    一.现象: pip list 显示出以下错误:     DEPRECATION: The default format will switch to columns in the future. Yo ...

  10. Vue--vue中常用的ECMAScript6语法

    1.对象的写法 es5中对象: {add:add,substrict:substrict} es6中对象: {add,substrict} 注意这种写法的属性名称和值变量是同一个名称才可以简写,否则要 ...