原文地址:http://www.jianshu.com/p/6f40dddd71a5

1.定义切面

下面我们就来定义一场舞台剧中观众的切面类Audience:

package com.spring.aop.service.aop;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

/**

* <dl>

* <dd>Description:观看演出的切面</dd>

* <dd>Company: 黑科技</dd>

年9月3日下午9:58:09</dd>

* <dd>@author:Kong</dd>

* </dl>

*/

@Aspect

public class Audience {

/**

* 目标方法执行之前调用

*/

@Before("execution(** com.spring.aop.service.perform(..))")

public void silenceCellPhone() {

System.out.println("Silencing cell phones");

}

/**

* 目标方法执行之前调用

*/

@Before("execution(** com.spring.aop.service.perform(..))")

public void takeSeats() {

System.out.println("Taking seats");

}

/**

* 目标方法执行完后调用

*/

@AfterReturning("execution(** com.spring.aop.service.perform(..))")

public void applause() {

System.out.println("CLAP CLAP CLAP");

}

/**

* 目标方法发生异常时调用

*/

@AfterThrowing("execution(** com.spring.aop.service.perform(..))")

public void demandRefund() {

System.out.println("Demanding a refund");

}

}

我们可以看到使用了几种注解,其实AspectJ提供了五中注解来定义通知:

注解

通知

@After

通知方法会在目标方法返回或抛出异常后调用

@AfterRetruening

通常方法会在目标方法返回后调用

@AfterThrowing

通知方法会在目标方法抛出异常后调用

@Around

通知方法将目标方法封装起来

@Before

通知方法会在目标方法执行之前执行

  聪明的你可能已经看到,同样的切点我们写了四遍,这是不科学的,强大的Spring怎么会没有处理的方法呢。其实我们可以使用@Pointcut注解声明一个通用的切点,在后面可以随意使用:

package com.spring.aop.service.aop;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

/**

* <dl>

* <dd>Description:观看演出的切面</dd>

* <dd>Company: 黑科技</dd>

年9月3日下午9:58:09</dd>

* <dd>@author:Kong</dd>

* </dl>

*/

@Aspect

public class Audience {

/**

* 定义一个公共的切点

*/

@Pointcut("execution(** com.spring.aop.service.Perfomance.perform(..))")

public void performance() {

}

/**

* 目标方法执行之前调用

*/

@Before("performance()")

public void silenceCellPhone() {

System.out.println("Silencing cell phones");

}

/**

* 目标方法执行之前调用

*/

@Before("performance()")

public void takeSeats() {

System.out.println("Taking seats");

}

/**

* 目标方法执行完后调用

*/

@AfterReturning("performance()")

public void applause() {

System.out.println("CLAP CLAP CLAP");

}

/**

* 目标方法发生异常时调用

*/

@AfterThrowing("performance()")

public void demandRefund() {

System.out.println("Demanding a refund");

}

}

  这样定义一个切点后,后面我们的方法想使用这个切点直接调用切点所在的方法就行了。实际上切面也是一个Java类,我们可以将它装配到Spring中的bean中:

/**

* 声明Audience bean

* @return

*/

@Bean

public Audience audience(){

return new Audience();

}

  但是现在Spring还不会将Audience视为一个切面,即便使用了@AspectJ注解,但它并不会被视为一个切面们这些注解不会被解析,也不会创建将其转化为切面的代理。但我们可以使用JavaConfig,然后在JavaConfig类上使用注解@EnableAspectJAutoProxy注解启动自动代理功能:

package com.spring.aop.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.EnableAspectJAutoProxy;

import com.spring.aop.service.aop.Audience;

/**

* <dl>

* <dd>Description:配置类</dd>

* <dd>Company: 黑科技</dd>

年9月3日下午10:20:11</dd>

* <dd>@author:Kong</dd>

* </dl>

*/

@Configuration

//启动AspectJ自动代理

@EnableAspectJAutoProxy

@ComponentScan

public class ConcertConfig {

/**

* 声明Audience bean

* @return

*/

@Bean

public Audience audience(){

return new Audience();

}

}

如果你想使用XML配置也是可以的,我们要使用Spring aop命名空间中的<aop:aspectj-autoproxy>元素:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/cache

http://www.springframework.org/schema/cache/spring-cache.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">

<context:component-scan base-package="com.spring.aop" />\

<!-- 启动AspectJ自动代理 -->

<aop:aspectj-autoproxy/>

<bean class="com.spring.aop.Audience" />

</beans>

  其实不管使用JAvaConfig还是Xml,AspectJ都会为使用@ApsectJ注解的Bean创建一个代理,这个代理会环绕着所有该切面所匹配的bean。

Spring AOP 使用注解定义切面(转载)的更多相关文章

  1. 利用Spring AOP自定义注解解决日志和签名校验

    转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...

  2. spring aop 使用注解方式总结

    spring aop的注解方式:和xml的配置方式略有区别,详细如下: 1.首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法. /** * */ package com ...

  3. (转)利用Spring AOP自定义注解解决日志和签名校验

    一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...

  4. spring-第十七篇之spring AOP基于注解的零配置方式

    1.基于注解的零配置方式 Aspect允许使用注解定义切面.切入点和增强处理,spring框架可以识别并根据这些注解来生成AOP代理.spring只是用了和AspectJ 5一样的注解,但并没有使用A ...

  5. spring AOP自定义注解方式实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  6. spring AOP自定义注解 实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  7. 基于注解的Spring AOP的配置和使用--转载

    AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  8. Spring AOP基于注解的“零配置”方式实现

    为了在Spring中启动@AspectJ支持,需要在类加载路径下新增两个AspectJ库:aspectjweaver.jar和aspectjrt.jar.除此之外,Spring AOP还需要依赖一个a ...

  9. 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)

    主题:这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力. public Merchant lo ...

随机推荐

  1. surface book2 添加自定义分辨率

    surface book2 13.5英寸  是3:2的屏幕, 因为默认分辨率3000*2000实在是太高了,看字的时候眼睛有点吃不消  即使开启windows的自定义缩放也有点难受,加上windows ...

  2. 从AWSome Day你可以学到什么?

    前言: AWS中国资深技术专家将带领你循序渐进的了解AWS主要核心服务,包括:计算(Compute).存储(Storage).数据库(Database).网络(Networking)以及安全性(Sec ...

  3. 使用 Blender* 重新拓扑 VR 和游戏素材

    本文介绍如何将网格重新拓扑成一个整洁的低密度模型,然后 UV 解包该网格,以便将纹理贴添加至新模型.本文还将探讨如何使用免费工具,比如 Blender* 及其 Bsurface 插件,重新拓扑雕塑的 ...

  4. python学习之函数(一)

    4.4函数 面向过程编程的缺点: ​ 1.代码重复: ​ 2.代码可描述性不高: 4.4.1 函数初识 ​ 函数是对代码块动作和功能的封装定义:函数是以功能为导向,一个为函数封装一个功能. ​ 优点: ...

  5. 源码搭建git,并连接github

    一.环境 1.下载源码包 https://mirrors.edge.kernel.org/pub/software/scm/git/ 2.安装编译环境 #yum install gcc gcc-c++ ...

  6. 【VS开发】【图像处理】Pleora推出iPORT CL-U3外置抓帧器

    全球领先的高性能视频接口产品供应商Pleora科技公司近日宣布推出可将Camera Link®摄像头转化为USB3Vision™摄像头的首个产品iPORT CL-U3外置抓帧器,树立了另一个行业里程碑 ...

  7. Redhat7 CentOS7安装阿里云的yum源

    先说CentOS的 1.备份 [root@harry ~]# cd /etc/yum.repos.d/ [root@harry yum.repos.d]# mkdir repo_bak [root@b ...

  8. awk 命令使用指南

    一:awk 的执行流程 (1) 首先执行BEGIN{} 块中的初始化操作 (2) 然后从指定的数据文件中循环读取一个数据行 ( 自动更新 NF.NR.$0.$1....., 等内建变量的值) (3) ...

  9. property可以声明得位置

    property可以声明的位置有4处 1 @interface处 2 扩展处 3 protocol处 4 分类处 其中分类处的property不会合成实例变量,并且编译器也不会自动合成实例变量

  10. 三校联训 小澳的葫芦(calabash) 题解

    题面:小澳的葫芦[ 题目描述]小澳最喜欢的歌曲就是<葫芦娃>.一日表演唱歌,他尽了洪荒之力,唱响心中圣歌.随之,小澳进入了葫芦世界.葫芦世界有 n 个葫芦,标号为 1~ n. n 个葫芦由 ...