玩转Spring——Spring AOP

什么是AOP?
AOP(Aspect Oriented Programming )被称为面向切面编程
AOP利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面。所谓"切面",简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。

AOP操作术语
1、横切关注点:
对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点。
2、切面(aspect):
OOP中类是对物体特征的抽象,那么切面就是AOP中对横切关注点的抽象,切面通常是一个类,里面可以定义切入点和通知(实际上就是你要增加的那段代码)。
3、连接点(joinpoint):
被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器。
4、切入点(pointcut):
对连接点进行拦截的定义,通过书写切入点表达式决定究竟要把哪些方法给拦截下来。
5、通知(advice):
所谓通知指的就是指拦截到连接点之后要执行的代码,一般来说就是对目标方法的增强,通知分为前置(在目标方法前执行)、后置(在目标方法后执行)、异常(在目标方法抛出异常后执行)、返回(在目标方法返回结果后执行)、环绕(可以同时实现前置和后置功能)通知五种类型。
6、目标对象:
代理的目标对象。
7、织入(weave):
将切面应用到目标对象并导致代理对象创建的过程。
8、引入(introduction):
在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段。

Spring对AOP的支持

在Spring中可以有以下三种方式来现:
1、使用ProxyFactoryBean配置和实现对应接口的配置方式(比较老旧经典的实现,但是现在已经几乎被废弃);
2、使用Spring来定义纯粹的POJO切面,即在对应的springXMl配置文件中通过 <aop:config>标签实现;
3、使用@AspectJ注解驱动切面实现(最常使用);

注意:使用的前提是要引入两个依赖jar : aopalliance-1.0.jar 和 aspectjweaver.jar

下面通过注解来演示AOP
(1)找到连接点,定义切入点

/*
*要被织入通知的方法(切入点)
*/
@Component
public class RoleServiceImpl implements IRoleService {
public void print() {
System.out.println(“ RoleServiceImpl. print() ");
}
}

(2)创建切面

@Component
@Aspect
public class XmlAspect {
//前置通知
@Before("execution(* aop.RoleServiceImpl.print(..))")
public void before(){ 
System.out.println("brfore........."); 
}
//后置通知
@After("execution(* aop.RoleServiceImpl.print(..))")
public void after(){ 
System.out.println("after........."); 
}
//异常通知
@AfterThrowing("execution(* aop.RoleServiceImpl.print(..))")
public void afterThrowing(){
System.out.println("afterThrowing.........");
}
//返回通知
@AfterReturning ("execution(* aop.RoleServiceImpl.print(..))")
public void afterReturing(){
System.out.println("afterReturing.........");
}
//环绕通知
@Around("execution(* aop.RoleServiceImpl.print(..))")
public void around(ProceedingJoinPoint pj){
System.out.println("环绕前");
try {
pj.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("环绕后");
}
}

(3)在xml中进行配置

<!--下面这句是重点-->
<aop:aspectj-autoproxy/>

(4)测试,我们就能看到结果。

(略)

如果我们不用注解,就只需要在xml中配置如下:

<bean id="roleServiceImpl" class="aop.RoleServiceImpl"/>
<!-- XmlAspect就是我们的切面实例,需要在下面被引入 -->
<bean id="xmlAspect" class="aop.XmlAspect"/>

<aop:config>
<aop:aspect ref="xmlAspect">
<aop:pointcut id="printId" expression="execution(* aop.RoleServiceImpl.print(..))"/>
<aop:before method="before" pointcut-ref="printId"/>
<aop:after method="after" pointcut-ref="printId"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="printId"/>
<aop:after-returning method="afterReturing" pointcut-ref="printId"/>
</aop:aspect>
</aop:config>

最后简单地说一下这里的execution正则表达式含义:
1、execution: 表示执行对应方法的时候会触发
2、* :代表任意类型返回类型的方法
3、aop.RoleServiceImpl: 代表类的全限定名
4、ptrint :代表被拦截的方法名字
5、(..) : 代表任意的参数。

 

原文地址:https://www.cnblogs.com/ericz2j/p/10908969.html