Spring实战学习笔记(三):AOP

AOP概述

在软件开发中,散布于应用中多处的功能(如安全,事务和日志模块等)被称为横切关注点。通常来讲,这些横切关注点从概念上是与应用的业务逻辑相分离的(但是往往会直接嵌入到应用的业务逻辑之中)。把这些横切关注点与业务逻辑相分离正是面向切面编程所要解决的问题。
横切关注点可以被模块化为特殊的类,这些类被称为切面(aspect )。这样做有两个好处:首先,现在每个关注点都集中于一个地方,而不是分散到多处代码中;其次,服务模块更简洁,因为它们只包含主要关注点(或核心功能)的代码,而次要关注点的代码被转移到切面中了。

通知(Advice)

在AOP术语中,切面的工作被称为通知。Spring切面可以应用5种类型的通知:

  • 前置通知(Before):在目标方法被调用之前调用通知功能
  • 后置通知(After):在目标方法被调用之后调用通知功能,此时不关心方法的输出是什么
  • 返回通知(After-returning):在目标方法成功执行之后调用通知功能
  • 异常通知(After-throwing):在目标方法抛出异常后调用通知
  • 环绕通知(Around):通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的方法
连接点(Join Point)

连接点是在应用执行过程中能够插入切面的一个点,也就是应用通知的时机。

切点(Poincut)

如果说通知定义了切面的“什么”和“何时”的话,那么切点就定义了“何处”。切点的定义会匹配通知所要织入的一个或多个连接点。我们通常使用明确的类和方法名称,或是利用正则表达式定义所匹配的类和方法名称来指定这些切点。

切面(Aspect)

切面是通知和切点的结合。通知和切点共同定义了切面的全部内容——它是什么,在何时和何处完成其功能。

织入(Weaving)

织入是把切面应用到目标对象并创建新的代理对象的过程。

基于注解的AOP实例

######1. 定义表演接口

package annotationaop;
public interface Performance {
	void perform();
}

######2. 表演者实现表演接口

package annotationaop;
public class Performer implements Performance{
	@Override
	public void perform() {
		System.out.println("Performer Perform");
	}
}

######3. 定义观看演出的切面

package annotationaop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect
public class Audience{
	//使用AspectJ的切点表达式语言来定义切点
	@Pointcut("execution(* annotationaop.Performance.perform(..))")
	public void performance(){}
	/*
	//非环绕通知
	@Before("performance()")
	public void silenceCellPhones(){
		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");
	}
	*/
	@Around("performance()")//采用环绕通知
	public void watchPerformance(ProceedingJoinPoint jp){
		try{
			System.out.println("Silencing cell phones");
			System.out.println("Taking seats");
			jp.proceed();//将控制权交给被通知的方法
			System.out.println("CLAP CLAP CLAP!!!");
		}catch (Throwable e){
			System.out.println("Demanding a refund");
		}
	}
}

######4. 配置bean

package annotationaop;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
//启用AspectJ注解的自动代理
@EnableAspectJAutoProxy
public class AOPAspectConfig {
	@Bean
	public Audience audience(){
		return new Audience();
	}
	@Bean
	public Performance performance(){
		return new Performer();
	}
}

######5. 测试

package annotationaop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AopAspectTest {
	public static void main(String[] args){
		ApplicationContext context = new AnnotationConfigApplicationContext(AOPAspectConfig.class);
		Performance performance = (Performance)context.getBean("performance");
		performance.perform();
	}
}

结果如下:

Silencing cell phones
Taking seats
Performer Perform
CLAP CLAP CLAP!!!

问题

因为Spring基于动态代理,所以Spring只支持方法连接点。这与一些其他的AOP框架是不同的,例如AspectJ和JBoss,除了方法切点,它们还提供了字段和构造器接入点。如果需要方法拦截之外的连接点拦截功能,那么我们可以利用Aspect来补充Spring AOP的功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值