为讲解例子,我们首先定义一个Performance接口:

 package aoptest;

 public interface Performance {
public void perform();
}

再定义一个该接口的实现:

 package aoptest;

 public class PianoPerform implements Performance {

     @Override
public void perform() {
// TODO Auto-generated method stub
System.out.println("i am playing piano");
} }

在创建切面之前,我们先来看一下切点表达式的用法,如图所示:

关于切点表达式的更多用法,可查看相关文档。

接着,我们使用注解定义一个切面,Audience类会在perform方法执行前后织入指定的方法

 package aoptest;

 import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*; @Aspect
public class Audience { @Pointcut("execution(** aoptest.Performance.perform(..))")
public void performance() {}
//performance()方法的实际内容并不重要,在这里它是空的。
//其实该方法本身只是一个标识,供@Pointcut注解依附
//不这样做的话,就需要在每个方法前都使用这个长点的表达式 @Before("performance()")
public void silenceCellPhones() {
System.out.println("Slience cell phones");
} @Before("performance()")
public void takeSeats() {
System.out.println("takeSeats");
} @AfterReturning("performance()")
public void applause() {
System.out.println("applause");
} @AfterThrowing("performance()")
public void demandRefund() {
System.out.println("demandRefund");
}
}
  • @Before:通知方法会在目标方法调用之前调用
  • @AfterReturning:通知方法在目标方法成功返回后调用
  • @AfterThrowing:通知方法在目标方法抛出异常后调用
  • @Around:通知方法会将目标方法封装起来



接着,进行测试,首先使用JavaConfig进行相关bean的配置:

 package aoptest;

 import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration
@EnableAspectJAutoProxy //启用aspectJ自动代理
@ComponentScan
public class AopConfig {
@Bean
public Audience audience() {
return new Audience();
} @Bean
Performance performance() {
return new PianoPerform();
}
}

然后,创建测试类:

 package aoptest;

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //用于在测试开始时自动创建Spring上下文
@RunWith(SpringJUnit4ClassRunner.class)
//告诉上下文需要在AopConfig中加载配置
@ContextConfiguration(classes = { AopConfig.class })
public class PerformTest {
@Autowired
public Audience audience;
@Autowired
public Performance performance;
@Test
public void play() {
performance.perform();
}
}

测试结果,符合预期:

现在,我们利用@Around创建环绕通知,重新实现切面,可以达到相同的效果:

package aoptest;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*; @Aspect
public class Audience { @Pointcut("execution(** aoptest.Performance.perform(..))")
public void performance() {}
//performance()方法的实际内容并不重要,在这里它是空的。
//其实该方法本身只是一个标识,供@Pointcut注解依附
//不这样做的话,就需要在每个方法前都使用这个长点的表达式 @Around("performance()")
//ProceedingJoinPoint这个对象是必须有的,因为需要通过它来调用被通知的方法,使用proceed()方法
public void watchPerformance(ProceedingJoinPoint jp) {
System.out.println("Slience cell phones");
System.out.println("takeSeats");
try {
jp.proceed();
} catch (Throwable e) {
System.out.println("demandRefund");
}
System.out.println("applause");
}
}

当然,你也可以不调用proceed()方法,从而阻塞对通知方法的访问。

Sping——使用注解创建切面的更多相关文章

  1. Spring AOP之使用注解创建切面

    上节中我们已经定义了Performance接口,他是切面中的切点的一个目标对象.那么现在就让我们使用AspectJ注解来定义切面吧. 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audien ...

  2. SpringInAction--面向切片的Spring以及如何使用注解创建切面

    什么叫做切片..什么叫做AOP... 与大多数技术一样,AOP已经形成了自己的术语.描述切面的常用术语有通知(advice).切点(pointcut)和连接点(join point). (一大串书上的 ...

  3. Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解

    引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...

  4. 基于@AspectJ注解配置切面与基于XML配置切面

    1. Waiter目标类 package com.smart.aop.advice.pointcut; public class Waiter { public void greetTo(String ...

  5. 010-Spring aop 001-核心说明-拦截指定类与方法、基于自定义注解的切面

    一.概述 面向切面编程(AOP)是针对面向对象编程(OOP)的补充,可以非侵入式的为多个不具有继承关系的对象引入相同的公共行为例如日志.安全.事务.性能监控等等.SpringAOP允许将公共行为从业务 ...

  6. 使用@Async注解创建多线程,自定义线程池

    说明 使用@Async注解创建多线程非常的方便,还可以通过配置,实现线程池.比直接使用线程池简单太多.而且在使用上跟普通方法没什么区别,加上个@Async注解即可实现异步调用. 用法 AsyncTas ...

  7. Spring Boot 使用 @Scheduled 注解创建定时任务

    在项目开发中我们经常需要一些定时任务来处理一些特殊的任务,比如定时检查订单的状态.定时同步数据等等. 在 Spring Boot 中使用 @Scheduled 注解创建定时任务非常简单,只需要两步操作 ...

  8. 【快学springboot】10.使用@Async注解创建多线程,自定义线程池

    说明 使用@Async注解创建多线程非常的方便,还可以通过配置,实现线程池.比直接使用线程池简单太多.而且在使用上跟普通方法没什么区别,加上个@Async注解即可实现异步调用. 用法 AsyncTas ...

  9. 自定义注解结合切面和spel表达式

    在我们的实际开发中可能存在这么一种情况,当方法参数中的某些条件成立的时候,需要执行一些逻辑处理,比如输出日志.而这些代码可能都是差不多的,那么这个时候就可以结合自定义注解加上切面加上spel表达式进行 ...

随机推荐

  1. 杭电 1548 A strange lift(广搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Others) ...

  2. Linux下一款可以使用命令行的pdf阅读器

    Zathura是linux下一款用命令行控制打pdf阅读器,并且基本打使用方法和vim很相似.对于喜欢键盘操作的用户来说的确是一个不错的选择. ubuntu下的安装命令: sudo apt-get i ...

  3. leetcode 217 Contains Duplicate 数组中是否有反复的数字

     Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...

  4. ios6--UILabel

    // // ViewController.m // 02-UILabel的使用 // // UILabel显示一段文字. #import "ViewController.h" @i ...

  5. Timus 1146. Maximum Sum

    1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...

  6. 排列(permutation) 用1,2,3,…,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要 求abc:def:ghi=1:2:3。按照“abc def ghi”的格式输出所有解,每行一个解。

    #include <stdio.h> #include <math.h> // 算法竞赛的目标是编程对任意输入均得到正确的结果. // 请先独立完成,如果有困难可以翻阅本书代码 ...

  7. 【线程安全】—— 单例类双重检查加锁(double-checked locking)

    1. 三个版本单例类的实现 版本1:经典版 public class Singleton { public static Singleton getInstance() { if (instance ...

  8. Black Rock Shooter

    在人气动漫 Black Rock shooter 中,当加贺里对麻陶 说出了"滚回去"以后,与此同时,在另一个心灵世界里, BRS 也遭到了敌人的攻击.此时,一共有 n 个攻击排成 ...

  9. MSP430:输入捕获

    在做超声模块时用到 //捕获上升沿 void Capture_Pos(void) { P2SEL |= Echo; //选择P23作为捕捉的输入端子 Timer1_A //TA1CCTL1 |=CM_ ...

  10. Scala 是一门怎样的语言,具有哪些优缺点?

    保罗·格雷厄姆在<黑客与画家>中写道,Java属于B&D(捆绑与束缚)类型的语言.为何束缚手脚?因为要让新手和明星程序员写出类似质量的代 码,尽可能的抹消人的才华对程序的影响.不同 ...