Sping——使用注解创建切面
为讲解例子,我们首先定义一个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——使用注解创建切面的更多相关文章
- Spring AOP之使用注解创建切面
上节中我们已经定义了Performance接口,他是切面中的切点的一个目标对象.那么现在就让我们使用AspectJ注解来定义切面吧. 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audien ...
- SpringInAction--面向切片的Spring以及如何使用注解创建切面
什么叫做切片..什么叫做AOP... 与大多数技术一样,AOP已经形成了自己的术语.描述切面的常用术语有通知(advice).切点(pointcut)和连接点(join point). (一大串书上的 ...
- Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解
引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...
- 基于@AspectJ注解配置切面与基于XML配置切面
1. Waiter目标类 package com.smart.aop.advice.pointcut; public class Waiter { public void greetTo(String ...
- 010-Spring aop 001-核心说明-拦截指定类与方法、基于自定义注解的切面
一.概述 面向切面编程(AOP)是针对面向对象编程(OOP)的补充,可以非侵入式的为多个不具有继承关系的对象引入相同的公共行为例如日志.安全.事务.性能监控等等.SpringAOP允许将公共行为从业务 ...
- 使用@Async注解创建多线程,自定义线程池
说明 使用@Async注解创建多线程非常的方便,还可以通过配置,实现线程池.比直接使用线程池简单太多.而且在使用上跟普通方法没什么区别,加上个@Async注解即可实现异步调用. 用法 AsyncTas ...
- Spring Boot 使用 @Scheduled 注解创建定时任务
在项目开发中我们经常需要一些定时任务来处理一些特殊的任务,比如定时检查订单的状态.定时同步数据等等. 在 Spring Boot 中使用 @Scheduled 注解创建定时任务非常简单,只需要两步操作 ...
- 【快学springboot】10.使用@Async注解创建多线程,自定义线程池
说明 使用@Async注解创建多线程非常的方便,还可以通过配置,实现线程池.比直接使用线程池简单太多.而且在使用上跟普通方法没什么区别,加上个@Async注解即可实现异步调用. 用法 AsyncTas ...
- 自定义注解结合切面和spel表达式
在我们的实际开发中可能存在这么一种情况,当方法参数中的某些条件成立的时候,需要执行一些逻辑处理,比如输出日志.而这些代码可能都是差不多的,那么这个时候就可以结合自定义注解加上切面加上spel表达式进行 ...
随机推荐
- Linux中W与Who命令的使用
踢掉一个从某个终端连上的用户pkill -kill -t pts/0 ---------------------------------------------------------------- ...
- 一个经典的消费者和生产者的实现(linux )
#include <stdio.h> #include <pthread.h> #define BUFFER_SIZE 16 // 缓冲区数量 struct ...
- 对话DDM:分布式数据库中间件全解析
进入云计算时代,传统的数据库在性能和容量等方面已无法满足企业的要求,随着数据量的不断骤增,易于扩展.拆分的数据库解决方案对于企业的云化转型更是显得尤为重要.为使企业应用上云更简单,分布式数据库中间件D ...
- ListView布局之View复用原理举例
1.简单介绍: ListView是android开发中经常使用的控件,系统自带的那些样式,我就不列举了. 今天主要看一下.一个模仿系统历史通话记录的ListView. 效果例如以下: 上面ListVi ...
- Linux下查看history里的某种命令
Linux下,直接键入history命令,会将当前账户此前所有的命令都显示出来,未免太多了些.如果我只想查找某种命令,怎么办? 比如说,我只想查找我之前运行过的 "git" 命令 ...
- _io.TextIOWrapper
''' SELECT * FROM Info_Roles WHERE Flag=1 LIMIT 2; select top y * from 表 where 主键 not in(select top ...
- 主表a主表b 从表c中有ab两个表中各一个字段a1,b1 从表d中有ab两个表中各一个字段a2,b2
a1和a2在a表中具有唯一性 b1和b2在b表中具有唯一性 现在需要连接c表和d表 需要分两步来做 1.先让c表join表a和表b select c.*,a.a2,b.b2 from c inner ...
- 排列(permutation) 用1,2,3,…,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要 求abc:def:ghi=1:2:3。按照“abc def ghi”的格式输出所有解,每行一个解。
#include <stdio.h> #include <math.h> // 算法竞赛的目标是编程对任意输入均得到正确的结果. // 请先独立完成,如果有困难可以翻阅本书代码 ...
- Android之利用EventBus进行数据传递
在项目中,不可避免的要在两个页面之间进行数据的传递,就算不传递,也需要进行刷新之类的,我们根据Google提供的库类方法,也是可以做的,主要有广播broadcastreceiver,startacti ...
- ES mapping可以修改include_in_all,也可以修改index_options,norm,但是无法修改_all属性!
ES mapping可以修改include_in_all,也可以修改index_options,norm,但是无法修改_all属性! curl -XPOST "http://localhos ...