Spring AOP

Spring AOP

引言

  • AOP(Aspect Orient Programming),我们一般称为面向方面(切面)编程,作为面向对象的一种补充,用于处理系统中分布于各个模块的横切关注点,比如事务管理、日志、缓存等等。AOP实现的关键在于AOP框架自动创建的AOP代理,AOP代理主要分为静态代理和动态代理,静态代理的代表为AspectJ;而动态代理则以Spring AOP为代表。
    所谓”切面”,简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。
  • 使用”横切”技术,AOP把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处基本相似,比如权限认证、日志、事物。AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。

使用spring AOP

  • Spring AOP使用的动态代理,所谓的动态代理就是说AOP框架不会去修改字节码,而是在内存中临时为方法生成一个AOP对象,这个AOP对象包含了目标对象的全部方法,并且在特定的切点做了增强处理,并回调原对象的方法。
  • AOP编程的关键就是定义切入点和定义增强处理,一旦定义了合适的切入点和增强处理,AOP框架将自动生成AOP代理即:代理对象的方法=增强处理+被代理对象的方法。

    • Spring AOP中的动态代理主要有两种方式,JDK动态代理和CGLIB动态代理。JDK动态代理通过反射来接收被代理的类,并且要求被代理的类必须实现一个接口。JDK动态代理的核心是InvocationHandler接口和Proxy类。
    • 如果目标类没有实现接口,那么Spring AOP会选择使用CGLIB来动态代理目标类。CGLIB(Code Generation Library),是一个代码生成的类库,可以在运行时动态的生成某个类的子类,注意,CGLIB是通过继承的方式做的动态代理,因此如果某个类被标记为final,那么它是无法使用CGLIB做动态代理的。
JDK动态代理是利用反射实现

用Spring AOP的XML实现方式

<?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:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

       <bean id="helloWorldImpl1" class="com.xrq.aop.HelloWorldImpl1" />
       <bean id="helloWorldImpl2" class="com.xrq.aop.HelloWorldImpl2" />
       <bean id="timeHandler" class="com.xrq.aop.TimeHandler" />
       <bean id="logHandler" class="com.xrq.aop.LogHandler" />

 <aop:config>
     <aop:aspect id="time" ref="横切关注点" order="1">
               <aop:pointcut id="横切关注点的方法名1" expression="execution(*目标对象.*(..))" />
               <aop:before method="前置通知" pointcut-ref="addTime" />
               <aop:after method="后置通知" pointcut-ref="addTime" />
     </aop:aspect>
     <aop:aspect id="log" ref="横切关注点" order="2">
               <aop:pointcut id="横切关注点的方法名2" expression="execution(* 目标对象.(可以具体到某个方法)*(..))" />
               <aop:before method="前置通知" pointcut-ref="printLog" />
               <aop:after method="后置通知" pointcut-ref="printLog" />
     </aop:aspect>
   </aop:config>
</beans>
  • aspect里面有一个order属性,order属性的数字就是横切关注点的顺序

用注解方式(需引用jar包:AspectJ相关的 jar 包 aopalliance-1.0.jar 和 aspectjweaver.jar)

  • 用@Aspect注解方式来实现前置通知、返回通知、后置通知、异常通知、环绕通知
/**
*@Aspect注解是不能够通过类路径自动检测发现的,所以需要配合使用@Component注释
**/

@Component
@Aspect
//@Aspect注解标识它为一个切面,并且将自己从自动代理中排除
public class LoggingAspect {
//切入点
    /**
    *Pointcut的定义包括两个部分:Pointcut表示式(expression)和Pointcut签名(signature)
    **/
 @Pointcut("execution(* com.alibaba.aop.PersonServerBean.*(..))")
    private void anyMethod(){}

    /**
    *然后要使用所定义的Pointcut时,可以指定Pointcut签名
    **/
    //前置通知:目标方法执行之前执行以下方法体的内容 
    @Before("anyMethod()")
    public void doAccessCheck() {
        System.out.println("前置通知");
    }
    //后置通知:目标方法执行之后执行以下方法体的内容,不管是否发生异常。
    @After("anyMethod()")
    public void after() {
        System.out.println("最终结果");
    }
    //返回通知:目标方法正常执行完毕时执行以下代码
    @AfterReturning("anyMethod()")
    public void doAfter() {
        System.out.println("后置通知");
    }
    //异常通知:目标方法发生异常的时候执行以下代码
    @AfterThrowing("anyMethod()")
    public void doAfterThrow() {
        System.out.println("例外通知");
    }
   // 环绕通知:目标方法执行前后分别执行一些代码,发生异常的时候执行另外一些代码
    @Around("anyMethod()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("进入环绕通知");
        Object object = pjp.proceed();
        System.out.println("退出方法");
        return object;
    }
    // 顺序:around-> before->method->after->afterReturning
    }

1)execution(* *(..))
//表示匹配所有方法
2)execution(public * com. savage.service.UserService.*(..))
//表示匹配com.savage.server.UserService中所有的公有方法
3)execution(* com.savage.server...(..))
//表示匹配com.savage.server包及其子包下的所有方法

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值